仅选择/计数已验证且未禁止用户

时间:2013-05-20 10:41:49

标签: php sql count

这是代码

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中不存在的已验证用户?

请问好吗?

2 个答案:

答案 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