SQL查询按天分组但具有其他条件

时间:2017-06-27 21:59:44

标签: mysql sql

我的数据是这样的:

| visitor_id | time       | my_flag |
| 1          | 01-01-2017 | 1       |
| 2          | 01-01-2017 | 1       |
| 1          | 01-02-2017 | 2       |
| 3          | 01-02-2017 | 2       |

等...

我希望在一天内对所有访客进行分组,但会根据标记进行分组。标志可以有两个值,1或2.所以我需要像

这样的东西
| 12 | 01-01-2017 | 1 |
| 10 | 01-01-2017 | 2 |
| 34 | 01-02-2017 | 1 |

我有

select v.visitor_id v.time, v.flag
from my_table as v
group by DAY(v.time) having flag=1;

但我无法弄清楚如何添加多个条件(flag = 1和flag = 2)?有什么指针吗?

2 个答案:

答案 0 :(得分:1)

尝试这样的事情

select v.visitor_id v.time, v.flag
from my_table as v
where v.flag in (1,2)
group by DAY(v.time), v.flag 

答案 1 :(得分:1)

SELECT v.time, 
       v.flag, 
       COUNT(v.visitor_id)
FROM my_table as v
GROUP BY v.time, 
         v.flag;`