我如何在下面选择的两行之间交换operator_id?
即。 id=@job_offer_id
的operator_id成为id=@job_received_id
的operator_id,反之亦然。
query += "SELECT operator_id FROM booking WHERE id=@job_offer_id;" +
"SELECT operator_id FROM booking WHERE id=@job_received_id";
我在这里包含了一个小提琴:http://sqlfiddle.com/#!9/cdc53/4
答案 0 :(得分:2)
尝试自我加入
update booking as o join booking as r
set o.operator_id = r.operator_id, r.operator_id = o.operator_id
where o.id = 2 and r.id = 3;
像这样
答案 1 :(得分:1)
尝试将其保存在变量上并再次将其用于其他查询;
/* SELECT operator_id FROM booking WHERE id=@job_offer_id; */
SELECT operator_id INTO @job_offer_id FROM booking WHERE id="2";
SELECT operator_id FROM booking WHERE id=@job_offer_id ;
/* SELECT operator_id FROM booking WHERE id=@job_received_id; */
SELECT operator_id INTO @job_received_id FROM booking WHERE id="3";
SELECT operator_id FROM booking WHERE id=@job_received_id ;