MySQL 不同,group_concat 和计数问题
MySQL distinct, group_concat and count question
我有一个table。
+----------+----------------+--------+
| orderid | barcode | status |
+----------+----------------+--------+
| R-R34184 | K2020101500001 | 1 |
| R-R34184 | K2020101500001 | 1 |
| R-R34184 | K2020101500001 | 0 |
| R-R34184 | K2020101500001 | 0 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500003 | 1 |
| R-R34184 | K2020101500003 | 1 |
| R-R34184 | K2020101500003 | 0 |
| R-R34184 | K2020101500003 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500008 | 0 |
| R-R34185 | K2020101500008 | 0 |
| R-R34185 | K2020101500008 | 0 |
| R-R34185 | K2020101500008 | 0 |
+----------+----------------+--------+
我想查询如下:
DISTINCT orderby、CONCAT 状态和 COUNT 状态
+----------+---------------+--------------+
| orderid | status_concat | status_count |
+----------+---------------+--------------+
| R-R34184 | 0,1,2 | 3 |
+----------+---------------+--------------+
| R-R34185 | 0 | 1 |
+----------+---------------+--------------+
我尝试了几个代码,任何子查询,分组,但都没有在一起。
如果有人能帮忙,谢谢。
那只是聚合函数 group by
和 distinct
group_concat()
和 count()
:
select orderid,
group_concat(distinct status order by status) status_concat,
count(distinct status) status_count
from mytable
group by orderid
我想你想要 distinct
:
select orderid, group_concat(distinct status order by status), count(distinct status)
from t
group by orderid;
我有一个table。
+----------+----------------+--------+
| orderid | barcode | status |
+----------+----------------+--------+
| R-R34184 | K2020101500001 | 1 |
| R-R34184 | K2020101500001 | 1 |
| R-R34184 | K2020101500001 | 0 |
| R-R34184 | K2020101500001 | 0 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500002 | 2 |
| R-R34184 | K2020101500003 | 1 |
| R-R34184 | K2020101500003 | 1 |
| R-R34184 | K2020101500003 | 0 |
| R-R34184 | K2020101500003 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500005 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500006 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500004 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500007 | 0 |
| R-R34185 | K2020101500008 | 0 |
| R-R34185 | K2020101500008 | 0 |
| R-R34185 | K2020101500008 | 0 |
| R-R34185 | K2020101500008 | 0 |
+----------+----------------+--------+
我想查询如下: DISTINCT orderby、CONCAT 状态和 COUNT 状态
+----------+---------------+--------------+
| orderid | status_concat | status_count |
+----------+---------------+--------------+
| R-R34184 | 0,1,2 | 3 |
+----------+---------------+--------------+
| R-R34185 | 0 | 1 |
+----------+---------------+--------------+
我尝试了几个代码,任何子查询,分组,但都没有在一起。 如果有人能帮忙,谢谢。
那只是聚合函数 group by
和 distinct
group_concat()
和 count()
:
select orderid,
group_concat(distinct status order by status) status_concat,
count(distinct status) status_count
from mytable
group by orderid
我想你想要 distinct
:
select orderid, group_concat(distinct status order by status), count(distinct status)
from t
group by orderid;