gpt4 book ai didi

mysql - 除了一列外所有行都相似

转载 作者:行者123 更新时间:2023-11-29 04:12:28 25 4
gpt4 key购买 nike

我不确定这叫什么,但它总是在您得到如下结果集时发生:

(id,name,age,favorite)
0,John Smith,21,Pepperoni
1,John Smith,21,Ham
2,John Smith,21,Olives
3,John Doe,54,Tomatoes
4,John Doe,54,Potatoes

现在,当您迭代此结果集时,如何连接具有相似 nameage 的行的最后一列?获得类似 John Smith,21,"Pepperoni,Ham,Olives" 的结果。这个 Action 一般叫什么?如果重要的话,我正在使用 JDBC-MySQL。

谢谢

最佳答案

这个 Action 可以用 MySQL 中的 GROUP_CONCAT 完成:

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

SELECT name, age, GROUP_CONCAT(favorite SEPARATOR ',')
FROM myTable
GROUP BY name, age

请注意,GROUP_CONCAT 仅在 MySQL 中存在,而不是标准 SQL。

在其他 DBMS 中,您无法通过查询做到这一点。

关于mysql - 除了一列外所有行都相似,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6533140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com