如果商店只有一个公司id,我有下面的查询来插入数据。如果已经有两个公司id,我不想插入数据。逻辑看起来很简单,但是如果有2个或更多的公司id,我不知道如何防止插入数据。在下面的查询中,它不断插入数据。我是一个sql的新手,所以一些例子或提示将是一个很大的帮助!我很想听到你的消息!
INSERT INTO customers (
id,
name,
phone,
mail,
address
)
SELECT
company.id,
company.name,
company.phone,
company.mail,
company.address,
FROM
company
LEFT JOIN shops ON company.id = shops.id
WHERE
shops.company_id IN (
SELECT
company.id
FROM
company
LEFT JOIN shops ON company.id = shops.company_id
WHERE
shops.id IN (1,2,41)
)
GROUP BY
shops.company_id
having
count(shops.company_id) >= 1 ;
暂无答案!
目前还没有任何答案,快来回答吧!