使用MySQL,我可以执行以下操作:
SELECT hobbies FROM peoples_hobbies WHERE person_id = 5;
我的输出:
shopping
fishing
coding
但我只想要1行1列:
预期输出:
shopping, fishing, coding
原因是我从多个表中选择了多个值,在所有的连接之后,我得到了比我想要的多得多的行。
我在MySQL Doc上查找了一个函数,它看起来不像CONCAT或CONCAT_WS函数接受结果集。
这里有人知道怎么做吗?
现在已经很晚了,但对于那些正在搜索“使用透视表将多个MySQL行连接到一个字段中”的人来说会很有帮助:)
查询:
SELECT pm.id, pm.name, GROUP_CONCAT(c.name) as channel_names
FROM payment_methods pm
LEFT JOIN payment_methods_channels_pivot pmcp ON pmcp.payment_method_id = pm.id
LEFT JOIN channels c ON c.id = pmcp.channel_id
GROUP BY pm.id
桌子
payment_methods
id | name
1 | PayPal
channels
id | name
1 | Google
2 | Faceook
payment_methods_channels_pivot
payment_method_id | channel_id
1 | 1
1 | 2
输出:
您可以使用GROUP_CONCAT:
SELECT person_id,
GROUP_CONCAT(hobbies SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;
正如Ludwig在评论中所说,您可以添加DISTINCT运算符以避免重复:
SELECT person_id,
GROUP_CONCAT(DISTINCT hobbies SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;
正如Jan在他们的评论中所说,您也可以在使用ORDER BY将值内爆之前对其进行排序:
SELECT person_id,
GROUP_CONCAT(hobbies ORDER BY hobbies ASC SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;
正如Dag在评论中所说,结果有1024字节的限制。要解决此问题,请在查询之前运行此查询:
SET group_concat_max_len = 2048;
当然,您可以根据需要更改2048。要计算和分配值:
SET group_concat_max_len = CAST(
(SELECT SUM(LENGTH(hobbies)) + COUNT(*) * LENGTH(', ')
FROM peoples_hobbies
GROUP BY person_id) AS UNSIGNED);
连接多个单独行的替代语法
警告:这篇文章会让你饿。
鉴于:
我发现自己想要选择多个单独的行,而不是一个组,并在某个字段上进行连接。
假设您有一个产品ID及其名称和价格表:
+------------+--------------------+-------+
| product_id | name | price |
+------------+--------------------+-------+
| 13 | Double Double | 5 |
| 14 | Neapolitan Shake | 2 |
| 15 | Animal Style Fries | 3 |
| 16 | Root Beer | 2 |
| 17 | Lame T-Shirt | 15 |
+------------+--------------------+-------+
然后,你有一些花哨的聊天ajax,将这些小狗列为复选框。
饥饿的河马用户选择13、15、16。她今天没有甜点。。。
查找:
一种用纯mysql在一行中总结用户订单的方法。
解决方案:
将GROUP_NCAT和IN子句一起使用:
mysql> SELECT GROUP_CONCAT(name SEPARATOR ' + ') AS order_summary FROM product WHERE product_id IN (13, 15, 16);
哪些输出:
+------------------------------------------------+
| order_summary |
+------------------------------------------------+
| Double Double + Animal Style Fries + Root Beer |
+------------------------------------------------+
奖金解决方案:
如果您也想要总价,请输入SUM():
mysql> SELECT GROUP_CONCAT(name SEPARATOR ' + ') AS order_summary, SUM(price) AS total FROM product WHERE product_id IN (13, 15, 16);
+------------------------------------------------+-------+
| order_summary | total |
+------------------------------------------------+-------+
| Double Double + Animal Style Fries + Root Beer | 10 |
+------------------------------------------------+-------+
这里,我的意图是在不使用group_concat()函数的情况下应用字符串连接:
Set @concatHobbies = '';
SELECT TRIM(LEADING ', ' FROM T.hobbies ) FROM
(
select
Id, @concatHobbies := concat_ws(', ',@concatHobbies,hobbies) as hobbies
from peoples_hobbies
)T
Order by Id DESC
LIMIT 1
Here
select
Id, @concatHobbies := concat_ws(', ',@concatHobbies,hobbies) as hobbies
from peoples_hobbies
将返回
Id hobbies
1 , shopping
2 , shopping, fishing
3 , shopping, fishing, coding
现在我们的预期结果是第三。所以我用
Order by Id DESC
LIMIT 1
然后我也将第一个“,”从字符串中删除
TRIM(LEADING ', ' FROM T.hobbies )