我具有以下表格结构
表1
+-----+---------+
| id | user_id |
+-----+---------+
| 1 | 123 |
| 2 | 456 |
+-----+---------+
表2
+-----+----------+------+------+
| id | user_id | data | name |
+-----+----------+------+------+
| 1 | 123 | abc | Tom |
| 2 | 123 | xyz | Jon |
| 3 | 456 | pqr | Johny|
+-----+----------+------+------+
我想在每个用户的 Table2 中列出数据计数,并将 data 列作为字符串添加到结果表中count列旁边。
我的SQL查询就像
SELECT t1.user_id, count(t2.id) as c
FROM Table1 t1
INNER JOIN Table2 t2 on t2.user_id = t1.user_id
这将数据列为
+---------+------+
| user_id | c |
+---------+------+
| 123 | 2 |
| 456 | 1 |
+---------+------+
但是我想要类似的数据
+---------+------+-------------+
| user_id | c | datas |
+---------+------+-------------+
| 123 | 2 | abc, xyz |
| 456 | 1 | pqr |
+---------+------+-------------+
如何选择用逗号联接的数据?
答案 0 :(得分:1)
使用GROUP_CONCAT
SELECT t1.user_id, GROUP_CONCAT(t2.data SEPARATOR ','),count(t2.id) as c
FROM Table1 t1
INNER JOIN Table2 t2 on t2.user_id = t1.user_id
group by t1.user_id
答案 1 :(得分:0)
我认为以下想法将与您匹配。
TranslationClass.GetDualTranslation("USA") // Returns US
TranslationClass.GetTripleTranslation("US") // Returns USA