我正在尝试将一些数据插入到表中,但仅在子查询COUNT为> 0
这是我到目前为止所做的。
INSERT INTO users_friends (userId, friendId) VALUES (77, 100)
WHERE
(SELECT COUNT(id) FROM users WHERE email = 'a@g.com') > 0
两个查询都独立工作。
这应该是一个简单的修复希望。干杯
答案 0 :(得分:5)
SQLFiddle demo if there are records 'a@g.com'
SQLFiddle demo if there are NOT records 'a@g.com'
INSERT INTO users_friends (userId, friendId)
SELECT 77, 100
FROM users WHERE email = 'a@g.com' LIMIT 1;
另一种方式是:
INSERT INTO users_friends (userId, friendId)
SELECT 77, 100
FROM dual
WHERE EXISTS
( SELECT * FROM users WHERE email = 'a@g.com' ) ;
答案 1 :(得分:2)
试试这个::
INSERT INTO users_friends (userId, friendId)
(SELECT 77, 100 FROM users GROUP BY email HAVING email= 'a@g.com' and count(id)>0)
答案 2 :(得分:0)
INSERT INTO users_friends (userId, friendId)
SELECT 77, 100 FROM users WHERE email = 'a@g.com'
GROUP BY email
HAVING COUNT(id) > 0