I’m wondering how to set a query to look for something in one table, and if it exists (or doesn’t exist) insert a row into another, otherwise do nothing. This is what I’ve got so far, which works for what I need it to do, but I need some kind of restriction on when this query is run:
INSERT IGNORE INTO jos_juga_u2g (user_id, group_id)
VALUES ({userid}, 4)
Basically, I need to look into a table which stores order info, and if a certain item_id exists, then run the query. Otherwise, do nothing.
Try (didn’t test it, and you have to add the correct name of the order table (I assumed the user_id is present in the order table))
INSERT IGNORE INTO jos_juga_u2g (user_id, group_id)
SELECT
user_id
, 4
FROM jos_whatever_the_order_table_is_called AS o
INNER JOIN jos_tienda_orderitems AS oi
ON oi.order_id = o.order_id
AND oi.product_id = 28
WHERE o.user_id = {userid}
LIMIT 1
Would that be the equivalent of an IF statement? IF a certain value exists in table A, insert a row into table B. IF the value does not exist, do nothing?
This is the query i’m trying to put together but since I’m weak with sql, I can’t make it work. This probably isn’t even the right syntax. I’m trying to put a user into a certain usergroup if a certain product is purchased.
SELECT product_id, order_id FROM jos_tienda_orderitems
IF (product_id=28)
THEN
INSERT IGNORE INTO jos_juga_u2g (user_id, group_id)
VALUES ({userid}, 4)