MySQL列数据以逗号分隔列表返回 [英] MySQL column data returned as comma delimited list
本文介绍了MySQL列数据以逗号分隔列表返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前有一个MySQL表,例如:
I currently have a MySQL table like:
id | friend
1 | 2
1 | 5
1 | 10
3 | 6
15 | 19
21 | 4
我正在尝试获取一个特定用户的所有朋友ID,并将其排列成逗号分隔的列表.例如,抓住user1的朋友,它将返回为
I'm trying to grab all the friend id's of one particular user and arrange them into a comma-delimited list. For example, grabbed user1's friends, it would return as
$friend_list = 2,5,10
当前,我有:
$sql = "SELECT friend FROM table__friends WHERE id = ".$user_id;
这只能抓一行..请帮助!
This only grabs one row though..please help!
谢谢
推荐答案
您要使用GROUP_CONCAT:
You want to use GROUP_CONCAT :
$sql = "SELECT GROUP_CONCAT(friend) FROM table__friends GROUP BY id HAVING id = ".$user_id;
根据更好的答案进行正确性调整.
Adjusted for correctness per the better answer.
这篇关于MySQL列数据以逗号分隔列表返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文