这是代码
SELECT username,count(username) FROM users WHERE status = '1' // $vuser to get the verified user only (it's the main query)
SELECT username FROM banned_users WHERE username = $vuser // if yes $bad_user is true else false
是否可以仅使用一个sql查询选择/统计banned_users中不存在的已验证用户?
请问好吗?
答案 0 :(得分:4)
SELECT u.username, count(u.username)
FROM users u
left outer join banned_users b on b.username = u.username
WHERE u.status = '1'
AND b.username is null
GROUP BY u.username
答案 1 :(得分:1)
尝试使用嵌套查询:
SELECT username, COUNT(username) // Selects the `username` and `COUNT(username)` columns
FROM users // From the `users` table
WHERE status = '1' // Where its `status` is '1'
AND username NOT IN (SELECT b.username FROM banned_users b) // And its `username` is not at `username` column in the `banned_users` table