如果在一个查询中的表中找不到记录,我可以插入数据吗? (如果是/否,请指导)
示例
SELECT * FROM CITIES ct WHERE ct.city = 'Any City Name';
如果上述查询返回ZERO [ 0 ]记录,则必须执行以下查询:
INSERT INTO CITIES (city) VALUES ('Any City Name');
SELECT * FROM CITIES ct WHERE ct.city = 'Any City Name'
答案 0 :(得分:2)
你可以试试这个:
INSERT INTO CITIES (city)
SELECT 'Any City Name'
FROM cities
WHERE NOT EXISTS (SELECT * FROM CITIES WHERE city = 'Any City Name') LIMIT 1;