I cannot get the code below to work. I'm trying to set a variable from a query and use it twice in another. The query runs fine if I copy the first query into the location of the variables. Since the first query will always return the same result I would prefer to not run it twice.
SET @sid = (SELECT status_id
FROM user_relationship_status
WHERE status = 'LIKE');
INSERT INTO user_relationships (user_id, target_user_id, status_id)
VALUES (
17,
22,
@sid
)
ON DUPLICATE KEY UPDATE
status_id = VALUES(@sid)
Also user_id and target_user_id are both primary keys.
SELECT @sid:=status_id FROM...