具有以下未取自任何其他查询的输入...
(891,892,903,993,1008)
我想为每个人(891
、892
等(制作以下INSERT
(XXXX
实际值(
INSERT INTO myTable (id, name)
SELECT
id,
name
FROM myTable
WHERE exp_id = XXXX AND cntr_id <> (SELECT id from plnt where id_emp = XXXX);
如何制作一个函数来将它们全部排成一行,而不是按如下方式逐个执行?
WHERE exp_id = 891 AND cntr_id <> (SELECT id from plnt where id_emp = 891);
然后WHERE exp_id = 892 AND cntr_id <> (SELECT id from plnt where id_emp = 892);
等等。
提前谢谢。
你不需要函数(它们不像你想象的那样工作(。但是,您可以联接表以消除子查询。
您可以像这样重写查询:
INSERT INTO myTable (id, name)
SELECT
id,
name
FROM your_table /*you were missing the from clause btw*/
LEFT JOIN plnt ON your_table.exp_id = plnt.id_emp
WHERE exp_id IN (891,892,903,993,1008) AND cntr_id <> plnt.id