2017-06-18 27 views
-2

我有一個快速的問題,我無法找到答案。如何加入/聯合查詢

(SELECT Student_Name AS 'First two from Quiz' FROM events WHERE Event_Name LIKE 'Quiz' ORDER BY Student_Name LIMIT 2) 
UNION ALL 
(SELECT Student_Name as 'Last two from Quiz' FROM (SELECT Student_Name FROM events WHERE Event_Name LIKE 'Quiz' ORDER BY Student_Name DESC LIMIT 2) as q ORDER BY Student_Name) 
UNION ALL 
(SELECT Student_Name as 'Last two from Sqlizer' FROM (SELECT Student_Name FROM events WHERE Event_Name LIKE 'Sqlizer' ORDER BY Student_Name DESC LIMIT 2) as w ORDER BY Student_Name) 
UNION ALL 
(SELECT Student_Name AS 'First two from Sqlizer' FROM events WHERE Event_Name LIKE 'Sqlizer' ORDER BY Student_Name LIMIT 2) 

這是我的一個小代碼。我需要把它們展示給另一個人。就像旁邊的一個一樣。由於所有工會都顯示在一列中。你有解決這個問題的想法嗎?請,這是時間問題。我想在2小時內完成它。

+1

你能不能改一下你的問題嗎?可能的話,預期的產量和實際可能會有所幫助 – FieryCat

+0

這是沒有必要的。問題已經被解決 –

回答

0

我很肯定有一個更優雅的解決方案,但這是我有時間想出來的。你可以使用連接來並排放置表,但是這會給你所有可能的數據排列,這是我們不想要的。所以我增加了一個計數器四個表,只有數據(一個在櫃檯都相等)的輸出一個排列的:

SELECT a.Student_Name AS 'First two from Quiz', b.Student_Name as 'Last two from Quiz', c.Student_Name as 'Last two from Sqlizer', d.Student_Name AS 'First two from Sqlizer' 
FROM 
(SELECT @s:[email protected]+1 serial_no, Student_Name FROM events, (select @s:=0) as s WHERE Event_Name LIKE 'Quiz' ORDER BY Student_Name LIMIT 2) as a, 
(SELECT @s1:[email protected]+1 serial_no, t2.Student_Name FROM (SELECT Student_Name FROM events WHERE Event_Name LIKE 'Quiz' ORDER BY Student_Name DESC LIMIT 2) as t2, (select @s1:=0) as s) as b, 
(SELECT @s2:[email protected]+1 serial_no, t3.Student_Name FROM (SELECT Student_Name FROM events WHERE Event_Name LIKE 'Sqlizer' ORDER BY Student_Name DESC LIMIT 2) as t3, (select @s2:=0) as s) as c, 
(SELECT @s3:[email protected]+1 serial_no, Student_Name FROM events, (select @s3:=0) as s WHERE Event_Name LIKE 'Sqlizer' ORDER BY Student_Name LIMIT 2) as d 

WHERE a.serial_no = b.serial_no and c.serial_no = d.serial_no and b.serial_no = c.serial_no 
+0

非常感謝你的解決方案!感謝您的快速響應!再次感謝你。祝你有個愉快的日子 –

+0

@ J.明天我很高興能夠幫助你。如果你喜歡這個答案,如果你能夠贊成並接受它作爲解決方案,這將有所幫助。 – jens108