[英]Insert value if value does not exist in another table
I would like to check if all values in column releases.id
exist in column charts_extended.release_id
我想检查在
releases.id
列中的所有值是否在charts_extended.release_id
列releases.id
存在
If the value does not exist then I want to insert that value in column releases.id
into column charts_extended.release_id
如果该值不存在,那么我想将该值插入
releases.id
列中,并插入charts_extended.release_id
列中
You should first come up with query to check whether the id exists in the table, for example: 您首先应该提出查询来检查表中是否存在该ID,例如:
SELECT id
FROM releases
LEFT JOIN charts_extended ON (release_id=releases.id)
WHERE release_id IS NULL;
If you are happy with that, you can proceed into converting it to an INSERT
statement: 如果对此感到满意,可以继续将其转换为
INSERT
语句:
INSERT INTO charts_extended (release_id)
SELECT id
FROM releases
LEFT JOIN charts_extended ON (release_id=releases.id)
WHERE release_id IS NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.