统计MySQL中的空数据
假设我有一张表,其中包含如下数据:
name status
bob single
bob single
jane null
tina null
shane married
我想要如果状态“单一或数据为空”则表示单一。因此,如果数据为空,脚本可以将其读取为单个数据,并且可以一起计数。 所以我可以显示如下结果:
Single Married
3 1
我已经尝试过这个,但它不起作用:
SELECT SUM(IF(status='single',1,0)) AS Single,
SUM(IF(status='married',1,0)) AS Married
FROM table
let say i have one table, which have data like:
name status
bob single
bob single
jane null
tina null
shane married
i want if status "single or data null" it means single. so if data empty script can read it as single and can count together.
so i can show result like:
Single Married
3 1
i have tried with this and it doesnt work:
SELECT SUM(IF(status='single',1,0)) AS Single,
SUM(IF(status='married',1,0)) AS Married
FROM table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用:
Use:
如果您知道只有“单身”、“已婚”和“空”作为选项,那么这将起作用:
否则请尝试
If you know there are only 'single', 'married', and null as options, this will work:
Otherwise try