0

这是代码

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 查询来选择/计算禁止用户中不存在的已验证用户?

请问有什么想法吗?

4

2 回答 2

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
于 2013-05-20T10:44:34.353 回答
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
于 2013-05-20T10:49:02.983 回答