开发者

does the order of columns in a SQL select matters?

my 开发者_运维知识库question is regarding a left join I've tried to count how many people are tracking a certain project. (there can be zero followers)

now the only way i can get it to work is by adding

group by idproject

my question is if the is a way to avoid using this and only selecting and implicitly

setting that group option.

SQL:

select `project_view`.`idproject` AS `idproject`,
count(`track`.`iduser`) AS `c`,`name`
from `project_view` left join `track` using(idproject)

I expected it count null as zero but it doesn't appear at all,

if i neglect counting then it shows as null where there are no followers.


If you have a WHERE clause to specify a certain project then you don't need a GROUP BY.

SELECT project_view.idproject, COUNT(track.iduser) AS c, name
FROM project_view
LEFT JOIN track USING (idproject)
WHERE idproject = 4

If you want a count for each project then you do need a GROUP BY.

SELECT project_view.idproject, COUNT(track.iduser) AS c, name
FROM project_view
LEFT JOIN track USING (idproject)
GROUP BY idproject


Yes the order of selecting matters. For performance reasons you (typically) want your most limiting select first to narrow your data set. This makes every subsequent query operate on a smaller dataset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜