Mysql 在一列中使用group by对同一id进行计数

Mysql 在一列中使用group by对同一id进行计数,mysql,Mysql,我有一张像这样的桌子 +------------+----------------+ | id_kondisi | id_sub_kondisi | +------------+----------------+ | 01 | 0102 | | 03 | 0302 | | 01 | 0101 | | 01 | 0102 | | 01 |

我有一张像这样的桌子

+------------+----------------+
| id_kondisi | id_sub_kondisi |
+------------+----------------+
| 01         | 0102           |
| 03         | 0302           |
| 01         | 0101           |
| 01         | 0102           |
| 01         | 0101           |
| 03         | 0301           |
| 03         | 0303           |
| 02         | 0202           |
| 01         | 0102           |
| 03         | 0301           |
| 01         | 0101           |
| 02         | 0203           |
| 03         | 0302           |
| 02         | 0202           |
| 02         | 0201           |
| 02         | 0202           |
+------------+----------------+
16 rows in set (0.00 sec)
+----------------+-------------+
| kondisi_tot    | coun_tot    |
+----------------+-------------+
| 01             |  6          |
| 0101           |  3          |
| 0102           |  3          |
| 02             |  5          |
| 0201           |  1          |
| 0202           |  3          |
| 0203           |  1          |
| 03             |  5          |
| 0301           |  2          |
| 0302           |  2          |
| 0303           |  1          |
+----------------+-------------+
我想要一个像这样的表格的结果

+------------+----------------+
| id_kondisi | id_sub_kondisi |
+------------+----------------+
| 01         | 0102           |
| 03         | 0302           |
| 01         | 0101           |
| 01         | 0102           |
| 01         | 0101           |
| 03         | 0301           |
| 03         | 0303           |
| 02         | 0202           |
| 01         | 0102           |
| 03         | 0301           |
| 01         | 0101           |
| 02         | 0203           |
| 03         | 0302           |
| 02         | 0202           |
| 02         | 0201           |
| 02         | 0202           |
+------------+----------------+
16 rows in set (0.00 sec)
+----------------+-------------+
| kondisi_tot    | coun_tot    |
+----------------+-------------+
| 01             |  6          |
| 0101           |  3          |
| 0102           |  3          |
| 02             |  5          |
| 0201           |  1          |
| 0202           |  3          |
| 0203           |  1          |
| 03             |  5          |
| 0301           |  2          |
| 0302           |  2          |
| 0303           |  1          |
+----------------+-------------+
所以我需要计算已经循环的数据id。就像上面的结果一样 我知道我必须使用
分组方式
,但如何将另一列变成一列

PS:My id\u kondisi和id\u sub\u kondisi是字符类型,而不是int类型

SELECT ID, COUNT(*) FROM (
   SELECT id_kondisi as ID FROM kondisi
   UNION ALL
   SELECT id_sub_kondisi as ID FROM kondisi
) sub
GROUP BY ID
ORDER BY ID

可能与订购有关,因为编号为02的ID将小于0102。也许您需要将ID转换为“ORDERBY”语句中的字符串。

类似的方法应该可以工作。请注意,
UNION ALL
用于确保对所有值进行计数;然后将ID值转换为
CHAR
,以使排序顺序正常工作

MySQL 5.6架构设置

CREATE TABLE kondisi
    (`id_kondisi` int, `id_sub_kondisi` int)
;

INSERT INTO kondisi
    (`id_kondisi`, `id_sub_kondisi`)
VALUES
    (01, 0102),
    (03, 0302),
    (01, 0101),
    (01, 0102),
    (01, 0101),
    (03, 0301),
    (03, 0303),
    (02, 0202),
    (01, 0102),
    (03, 0301),
    (01, 0101),
    (02, 0203),
    (03, 0302),
    (02, 0202),
    (02, 0201),
    (02, 0202)
;
select id, count(id) 
from 
(select id_kondisi as id from kondisi
union all
select id_sub_kondisi from kondisi) merged_table
group by id
order by cast(id as char) 
    |  id | count(id) |
    |-----|-----------|
    |   1 |         6 |
    | 101 |         3 |
    | 102 |         3 |
    |   2 |         5 |
    | 201 |         1 |
    | 202 |         3 |
    | 203 |         1 |
    |   3 |         5 |
    | 301 |         2 |
    | 302 |         2 |
    | 303 |         1 |
查询1

CREATE TABLE kondisi
    (`id_kondisi` int, `id_sub_kondisi` int)
;

INSERT INTO kondisi
    (`id_kondisi`, `id_sub_kondisi`)
VALUES
    (01, 0102),
    (03, 0302),
    (01, 0101),
    (01, 0102),
    (01, 0101),
    (03, 0301),
    (03, 0303),
    (02, 0202),
    (01, 0102),
    (03, 0301),
    (01, 0101),
    (02, 0203),
    (03, 0302),
    (02, 0202),
    (02, 0201),
    (02, 0202)
;
select id, count(id) 
from 
(select id_kondisi as id from kondisi
union all
select id_sub_kondisi from kondisi) merged_table
group by id
order by cast(id as char) 
    |  id | count(id) |
    |-----|-----------|
    |   1 |         6 |
    | 101 |         3 |
    | 102 |         3 |
    |   2 |         5 |
    | 201 |         1 |
    | 202 |         3 |
    | 203 |         1 |
    |   3 |         5 |
    | 301 |         2 |
    | 302 |         2 |
    | 303 |         1 |

CREATE TABLE kondisi
    (`id_kondisi` int, `id_sub_kondisi` int)
;

INSERT INTO kondisi
    (`id_kondisi`, `id_sub_kondisi`)
VALUES
    (01, 0102),
    (03, 0302),
    (01, 0101),
    (01, 0102),
    (01, 0101),
    (03, 0301),
    (03, 0303),
    (02, 0202),
    (01, 0102),
    (03, 0301),
    (01, 0101),
    (02, 0203),
    (03, 0302),
    (02, 0202),
    (02, 0201),
    (02, 0202)
;
select id, count(id) 
from 
(select id_kondisi as id from kondisi
union all
select id_sub_kondisi from kondisi) merged_table
group by id
order by cast(id as char) 
    |  id | count(id) |
    |-----|-----------|
    |   1 |         6 |
    | 101 |         3 |
    | 102 |         3 |
    |   2 |         5 |
    | 201 |         1 |
    | 202 |         3 |
    | 203 |         1 |
    |   3 |         5 |
    | 301 |         2 |
    | 302 |         2 |
    | 303 |         1 |

您需要使用
UNION
组合这两列的结果集:

以下联合查询返回两列的结果:

SELECT usr_user_type_removed as id from user UNION ALL select usr_status from user
完整查询:

SELECT id, COUNT(id) FROM 
(SELECT usr_user_type_removed as id from user UNION ALL select usr_status from user ) n_table
group by id

我想按asc下订单,id是achar/varchar类型为什么不算数?对于count(*)中的每一行结果,它只返回到“1”,但实际上,它将两列合并为一列的结果非常糟糕。我的id是字符类型,不是int,我刚刚编辑了我的问题。如果数据类型是char类型,有什么方法吗?