我有像这样的表 kondisi
private void navigateToDrawerItems(Class cls) {
drawerLayout.closeDrawer(GravityCompat.END);
Intent intent = new Intent(this, cls);
if (((MyOwnApplication)getApplication()).isActivityInBackStack(cls)) {
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
startActivity(intent);
overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
}
}
我想要一个看起来像这样的表的结果
+------------+----------------+
| 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)
所以我需要计算已循环的数据ID。就像上面的结果一样
我知道我必须使用+----------------+-------------+
| 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 |
+----------------+-------------+
但是如何将另一列放入一列呢?
PS:我的id_kondisi和id_sub_kondisi是char类型,而不是int类型
答案 0 :(得分:2)
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
可以与订购有关因为ID为02,将小于0102.可能需要在“order by”语句中将ID转换为字符串。
答案 1 :(得分:1)
这样的事情应该有效。请注意,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)
;
查询1 :
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)
<强> Results 强>:
| 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 |
答案 2 :(得分:1)
您需要使用UNION
以下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