Consider following tables:

**product**

```
+----+------+-------------+
| id | name | category_id |
+----+------+-------------+
| 1  | foo  |           1 |
+----+------+-------------+

```

**categories**

```
+----+------+
| id | name |
+----+------+
| 1  | food |
+----+------+

```

Now lets assume I someone POSTs a new product:

```
{
  "name": "bar",
  "category": "drink"
}

```

In this case we need to create the new category automatically:

```
INSERT IGNORE INTO categories (name) VALUES ('drink')

```

Then we finally could insert the actual product row:

```
INSERT INTO products (name, category_id) VALUES ('bar', SELECT id FROM categories WHERE name = 'drink')

```

**However though this works it would require a transaction setup to be safe and as this does not seem to be a super complicated query I would like to know if it would be possible to merge both queries together (e.g. put the insert query of the categories into the select subquery of the product insertion)?**