gpt4 book ai didi

mysql - 实现 Mysql 连接。如何获得同一行的结果?

转载 作者:行者123 更新时间:2023-11-29 21:59:50 26 4
gpt4 key购买 nike

Er 图链接:

我想从子表中获取数据。例如。我需要所有 user_id 的位置、设备、语言、服务。对于给定的 user_id,位置、设备、语言和服务有多个值。

我尝试了以下代码:

SELECT  user_detail.user_id , user_detail.name , user_equipment.equipment , user_location.location , user_service.service , user_language.language
from user_detail
INNER JOIN user_equipment on user_detail.user_id = user_equipment.user_id
INNER JOIN user_location on user_detail.user_id = user_location.user_id
INNER JOIN user_service on user_detail.user_id = user_service.user_id
INNER JOIN user_language on user_detail.user_id = user_language.user_id;

现在的问题是,如果 user_id 有三个位置,我得到三行,有什么方法可以让所有位置都在同一行中?

最佳答案

您正在寻找此“GROUP_CONCAT”,您的查询将如下所示:

SELECT  user_detail.user_id ,
user_detail.name,
GROUP_CONCAT(user_equipment.equipment SEPARATOR ',') as equipments,
GROUP_CONCAT(user_location.location SEPARATOR ',') as locations,
GROUP_CONCAT(user_service.service SEPARATOR ',') as services,
GROUP_CONCAT(user_language.language SEPARATOR ',') as languages
FROM user_detail
INNER JOIN user_equipment on user_detail.user_id = user_equipment.user_id
INNER JOIN user_location on user_detail.user_id = user_location.user_id
INNER JOIN user_service on user_detail.user_id = user_service.user_id
INNER JOIN user_language on user_detail.user_id = user_language.user_id;
GROUP BY user_detail.user_id

关于mysql - 实现 Mysql 连接。如何获得同一行的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757681/

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