作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定这叫什么,但它总是在您得到如下结果集时发生:
(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
现在,当您迭代此结果集时,如何连接具有相似 name
和 age
的行的最后一列?获得类似 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/
我是一名优秀的程序员,十分优秀!