Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更高效的MySQL语句?_Mysql - Fatal编程技术网

更高效的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 有什么建议吗?

我有一个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 
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中有条目的用户?这是内部联接的默认行为。它是内部联接,甚至不需要有,因为只选择了具有匹配用户名的行。