简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM