MySql以单行显示详细信息

时间:2015-09-24 04:19:56

标签: mysql

我有一个查询,它返回多行中的特定用户数据,如下所示

enter image description here

上面的查询结果如下,

SELECT user_id,route_id,route_schedule_id FROM um.user_rm.route WHERE user_id IN (    SELECT UR.user_id FROM um.user_rm.route UR              INNER JOIN um.user_waitinglist UW ON UW.user_id = UR.user_id              WHERE UR.route_schedule_id = 43 );

我的要求是将详细信息排在单行中,如下所示

enter image description here

任何建议,

提前致谢 桑杰塔

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT user_id, route_id, CAST(GROUP_CONCAT(route_schedule_id) AS CHAR) AS schedules
FROM um.user_rm.route
WHERE user_id IN (
    SELECT UR.user_id
    FROM um.user_rm.route UR
    INNER JOIN um.user_waitinglist UW ON UW.user_id=UR.user_id WHERE UR.route_schedule_id=43
)
GROUP BY user_id

答案 1 :(得分:0)

我成功完成了查询并根据需要获得了输出。以下是我的查询,

SELECT R1.user_id,R1.route_id AS Route1,R1.route_schedule_id AS Schedule1,
       R2.route_id AS Route2,R2.route_schedule_id AS Schedule2 FROM `um.user_rm.route` R1 
INNER JOIN `um.user_rm.route` R2 ON R1.user_id=R2.user_id AND R1.user_route_id > R2.user_route_id
WHERE R1.user_id  IN
(
  SELECT UR.user_id FROM `um.user_rm.route` UR
             INNER JOIN `um.user_waitinglist` UW ON UW.user_id=UR.user_id
             WHERE UR.route_schedule_id=43
)
GROUP BY R1.user_id