如果找不到记录,MySQL计数返回零 [英] Mysql count return Zero if no record found

查看:102
本文介绍了如果找不到记录,MySQL计数返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个桌子.

cities - id_city, city_name
properties - id_property, id_city, property_name

我要显示cities.city_name并在其旁边显示[properties.count(id_city)]

I want to display cities.city_name and next to it [properties.count(id_city)]

如果没有找到任何记录而不是NULL,如何进行查询仍返回零的查询,这样我将得到如下结果:

How do I make a query that still returns zero if no records are found instead of NULL, so that I get results like this:

London [123]
New York [0]
Berlin [11]

其中纽约"是[0],而不是NULL而不是1?

where "New York" is [0], not NULL and not 1?

推荐答案

使用外部联接:

select cities.city_name, count(properties.id_city)
  from cities left join properties on cities.id_city = properties.id_city
  group by 1

这篇关于如果找不到记录,MySQL计数返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆