如何将两个不同分组(group by)的结果拼成逗号隔开的字符串

Group 1:

mysql> select group_concat(ssh_id)  from sm_attr where rule_id =1 group by rule_id;
+----------------------------------+
| group_concat(ssh_id)             |
+----------------------------------+
| 9,2,11,4,13,6,8,1,10,3,12,5,14,7 |
+----------------------------------+

Group 2:

mysql> select group_concat(ssh_id)  from sm_attr where rule_id =3 group by rule_id;
+----------------------+
| group_concat(ssh_id) |
+----------------------+
| 30,31                |
+----------------------+

拼成字符串:concate_ws():

mysql> select  concat_ws(',',tab1.general,tab2.xcache) as sshid  from (select group_concat(ssh_id) as  general from sm_attr where rule_id =1 group by rule_id) as tab1 ,(select group_concat(ssh_id) as  xcache from sm_attr where rule_id =3 group by rule_id) as tab2;
+----------------------------------------+
| sshid                                  |
+----------------------------------------+
| 9,2,11,4,13,6,8,1,10,3,12,5,14,7,30,31 |
+----------------------------------------+




你可能感兴趣的:(MySQL)