一尘不染

如何通过查询在mySQL组中显示计数为零的行?

sql

我尝试寻找类似的解决方案,但对我来说似乎并没有太大帮助。我有这样一个表:

locations => |longname|name|locations|

topics    => |longname|name|tag|

并且我正在尝试获取atag针对特定位置的长名称的数量。到目前为止,我所查询的内容也尝试强制重新排序,这就是为什么您看到location INand的原因FIELD。但是,如果某个位置没有人要使用特定标签,则会完全从响应中删除该标签。我希望它返回0。我该怎么做?

select count(b.longname) as count, a.location 
 from `topics` b, locations a 
  where tag = 'atag' and a.longname = b.longname 
  and location IN ('Japan', 'France', 'Italy') 
  group by location 
  order by FIELD(location, 'Japan', 'France', 'Italy')

阅读 405

收藏
2021-03-08

共1个答案

一尘不染

也许这个?:

SELECT COUNT(t.longname) as cnt, l.location 
FROM locations l 
  LEFT JOIN topics t
    ON  t.longname = l.longname
    AND t.tag = 'atag'      
WHERE l.location IN ('Japan', 'France', 'Italy') 
GROUP BY l.location 
ORDER BY FIELD(l.location, 'Japan', 'France', 'Italy')
2021-03-08