Lassen Sie uns Ihren Tisch anrufen, User_Types
.
Hier ist die Anweisung eine Zeile mit Beispieldaten einfügen (5,19,1), wenn LIRFU.type = 0
:
INSERT INTO User_Types (userId, locationId, type)
SELECT @userId:=5, 19, 1 from dual
WHERE (SELECT type FROM User_Types WHERE userId = @userId ORDER BY timestamp DESC LIMIT 1) = 0;
Sie diese in eine vorbereitete Anweisung in PHP machen können, indem die Daten mit ?
ersetzen.
$stmt = $db->prepare("INSERT INTO User_Types (userId, locationId, type) SELECT @userId:=?, ?, 1 from dual WHERE (SELECT type FROM User_Types WHERE userId = @userId ORDER BY timestamp DESC LIMIT 1) = 0");
$stmt->bind_param('ii', $user_id, $location_id);
//if LIRFU.type = 0
$stmt->execute();
Hier ist die Anweisung eine Zeile mit Beispieldaten einfügen (5,19,0), wenn LIRFU.type = 1 AND LIRFU.locationId != 19
:
INSERT INTO User_Types (userId, locationId, type)
SELECT @userId:=5, @locationId:=19, 0 from dual
WHERE (SELECT type FROM User_Types WHERE userId = @userId ORDER BY timestamp DESC LIMIT 1) = 1
AND (SELECT locationId FROM User_Types WHERE userId = @userId ORDER BY timestamp DESC LIMIT 1) <> @locationId;
Die gleiche Idee, wenn es in PHP hergestellt:
$stmt = $db->prepare("INSERT INTO User_Types (userId, locationId, type) SELECT @userId:=?, @locationId:=?, 0 from dual WHERE (SELECT type FROM User_Types WHERE userId = @userId ORDER BY timestamp DESC LIMIT 1) = 1 AND (SELECT locationId FROM User_Types WHERE userId = @userId ORDER BY timestamp DESC LIMIT 1) <> @locationId");
$stmt->bind_param('ii', $user_id, $location_id);
//if LIRFU.type = 1 and LIRFU.locationId != $location_id
$stmt->execute();