我使用了函数ON DUPLICATE KEY UPDATE。这很奇怪,因为在我的其他代码上,它运行得很好,但有了这个,我仍然可以得到相同的记录。
这是我的存储过程。
IF ((SELECT COUNT(playlist_id) FROM playlists WHERE playlist_id = i_playlistid) > 0) THEN
INSERT INTO playlist_terminal (playlist_id,terminal_id,userid,booking_id) VALUES (i_playlistid,i_terminal_id,i_userid,i_booking_id)
ON DUPLICATE KEY UPDATE terminal_id = i_terminal_id;
SELECT terminal.terminal_id,terminal.plate_number,terminal.terminal_status,
terminal.location, terminal_sum.booking_id FROM terminal
INNER JOIN playlist_terminal
ON terminal.terminal_id = playlist_terminal.terminal_id
INNER JOIN terminal_sum
ON playlist_terminal.terminal_id = terminal_sum.terminal_id
WHERE playlist_terminal.userid = i_userid AND playlist_id = i_playlistid;