更高效的MySQL语句?
我有一个mysql,它可以工作,但是我忍不住想有更好的方法吗更高效的MySQL语句?,mysql,Mysql,我有一个mysql,它可以工作,但是我忍不住想有更好的方法吗 SELECT u.username, count( c.username ) AS intImageCount FROM users u JOIN content c ON c.username = u.username WHERE (SELECT count( username )FROM content WHERE username = c.username) > 0 GROUP BY u.username 有什么建议吗?
SELECT u.username, count( c.username ) AS intImageCount
FROM users u
JOIN content c ON c.username = u.username
WHERE (SELECT count( username )FROM content WHERE username = c.username) > 0
GROUP BY u.username
有什么建议吗?试着用一个有意思的词
差不多
SELECT u.username, count( c.username ) AS intImageCount
FROM users u
JOIN content c ON c.username = u.username
GROUP BY u.username
HAVING count( c.username ) > 0
看一看而不是仅仅拥有,你已经做到了。 您可以完全删除子查询
SELECT u.username
, count( c.username ) AS intImageCount
FROM users u
INNER JOIN content c ON c.username = u.username
GROUP BY u.username
您看不到有必要进行子选择并使用左联接:
SELECT u.username, count( c.username ) AS intImageCount
FROM users u
LEFT JOIN content c ON c.username = u.username
GROUP BY u.username
如果我没有子查询,我如何只带回在content c中有条目的用户?这是内部联接的默认行为。它是内部联接,甚至不需要有,因为只选择了具有匹配用户名的行。