将多行加入一行

时间:2016-12-14 11:50:52

标签: mysql group-by

请帮助

id username date   place_type sangat mode mode_name mode_arrival satsang_taken

1  homm 2016-08-01  C          1500   SK     AKY     08:32:00       VHJ
2  homm 2016-08-03  C          1220   SK     AKY     07:30:00       ABC
3  plk  2016-08-12  C          1508   SK     AKJ     08:10:00       ABb

必填结果

 username date   place_type sangat mode mode_name mode_arrival satsang_taken   date   place_type sangat mode mode_name mode_arrival satsang_taken 
   homm   2016-08-01  C      1500   SK     AKY     08:32:00       VHJ       2016-08-03  C          1220   SK     AKY     07:30:00       ABC
   plk  2016-08-12    C        1508   SK   AKJ     08:10:00       ABb 

基本上可以存在任何可以存在的用户的行,以便所有行都在同一行中。

SELECT o.*, o1.* FROM maindb o INNER JOIN maindb o1 ON o.username = o1.username AND o.date!=o1.date

这基本上做得不好

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT o.*, o1.* FROM maindb o LEFT JOIN maindb o1 ON o.username = o1.username AND o.date!=o1.date where o.id = (select min(id) from maindb o2 where o2.username = o.username)

答案 1 :(得分:0)

使用内部联接创建视图

CREATE or REPLACE VIEW object(u_name,1_week,1_date,1_placet,1_sangat,1_mode ,1_mname,1_marr,1_stkn,1_sname,1_sbegin,1_s_end,1_day,2_week,2_date,2_placet,2_sangat,2_mode ,2_mname,2_marr,2_stkn,2_sname,2_sbegin,2_s_end,2_day,3_week,3_date,3_placet,3_sangat,3_mode ,3_mname,3_marr,3_stkn,3_sname,3_sbegin,3_s_end,3_day) AS 

(SELECT DISTINCT
maindb.username,
p1.week,p1.date,p1.place_type,p1.sangat,p1.mode,p1.mode_name,p1.mode_arrival,p1.satsang_taken,p1.saint_name,p1.satsangbegintime,p1.satsangendtime,p1.today,
p2.week,p2.date,p2.place_type,p2.sangat,p2.mode,p2.mode_name,p2.mode_arrival,p2.satsang_taken,p2.saint_name,p2.satsangbegintime,p2.satsangendtime,p2.today,
p3.week,p3.date,p3.place_type,p3.sangat,p3.mode,p3.mode_name,p3.mode_arrival,p3.satsang_taken,p3.saint_name,p3.satsangbegintime,p3.satsangendtime,p3.today

FROM maindb

INNER JOIN maindb AS p1 ON p1.username=maindb.username AND p1.week=1
INNER JOIN maindb AS p2 ON p2.username=maindb.username AND p2.week=2
INNER JOIN maindb AS p3 ON p3.username=maindb.username AND p3.week=3)