我正在寻找一个SQL语句来实现以下目标:
我有一张桌子像{ida | idb | count | direct}
我想在表中插入行,从另一个表中获取id a和b,count和direct是给定的值。我试过这样的方法:
不工作:

INSERT INTO my_tbl (ida, idb, count, direct)
SELECT id FROM other_tbl WHERE word ='test'
UNION
SELECT id FROM other_tbl WHERE word ='tost' 23, 5;

提前谢谢。

最佳答案

这就是你想要的吗?

INSERT INTO my_tbl (ida, idb, count, direct)
    SELECT o1.id, o2.id, 23, 5
    FROM other_tbl o1 JOIN
         other_tbl o2
         ON o1.word = 'test' AND o2.word = 'tost';

10-07 17:07