mysql查询不起作用 [英] mysql query not working

查看:143
本文介绍了mysql查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把查询写成

i wrote the query as

select id,count(id),flag from mytable group by id





但我得到的结果是单栏



我的桌子是



but i get the result in single column

my table is

ID | flag
1  | 0
2  | 0
3  | 1
2  | 0
1  | 0
3  | 1
2  | 0
1  | 1
2  | 1
3  | 1





我希望报告为此



i want the report as this

ID | Count Flag O | Count Flag 1
1  |    2         | 1
2  |    3         | 1
3  |    0         | 3







请帮助我...提前提前:))




please help me... thnks in advance :)

推荐答案

Select ID, SUM(case when flag = 0 then 1 else 0 end) as [Count Flag O], SUM(case when flag = 1 then 1 else 0 end) as [Count Flag 1] from test group by ID


SELECT  ID,
        SUM(IF(`flag` = 0, 1, 0)) 'Count Flag 0',
        SUM(IF(`flag` = 1, 1, 0)) 'Count Flag 1'
FROM    MyTable
GROUP   BY ID;


您可以使用case语句来实现此目的。
You can use case statement for achieving this.


这篇关于mysql查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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