我尝试寻找类似的解决方案,但对我来说似乎并没有太大帮助。我有这样一个表:
locations => |longname|name|locations| topics => |longname|name|tag|
并且我正在尝试获取atag针对特定位置的长名称的数量。到目前为止,我所查询的内容也尝试强制重新排序,这就是为什么您看到location INand的原因FIELD。但是,如果某个位置没有人要使用特定标签,则会完全从响应中删除该标签。我希望它返回0。我该怎么做?
atag
location IN
FIELD
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')
也许这个?:
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')