2つの異なるグループの結果をカンマで区切られた文字列につづる方法

1098 ワード

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 |
+----------------------------------------+