你可以使用INSERT...SELECT
语句
INSERT INTO tableName (project_ID)
SELECT P.project_id
FROM projects P
LEFT JOIN milestones M
USING(project_id)
GROUP BY P.project_id
HAVING count < 1
更改tableNAme
为要插入新行的表的名称。
更新 1
INSERT INTO tableName (project_ID, colA, colB, colC)
SELECT P.project_id, colA, colB, colC
FROM projects P
LEFT JOIN milestones M
USING(project_id)
GROUP BY P.project_id
HAVING count < 1
现在问题出现了,如果你想要一个列中不存在的值怎么办?您可以为此创建虚拟值。
INSERT INTO tableName (project_ID, field1, field2, field3)
SELECT P.project_id, '' AS field1, 0 AS field2, NULL AS field3
FROM projects P
LEFT JOIN milestones M
USING(project_id)
GROUP BY P.project_id
HAVING count < 1