2014-01-17 50 views
3

我有這樣的SQL:如何獲得MySQL的結果:[select * from mytable AS t]?

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test 
FROM 
(SELECT * FROM mytable) AS m1, 
(SELECT * FROM mytable) AS m2, 
(SELECT * FROM mytable) AS m3, 
(SELECT * FROM mytable) AS m4, 
(SELECT * FROM mytable) AS m5 
WHERE m1.id1=m2.id2 AND m1.id1=m3.id3 AND m1.id1=m4.id4 AND m1.id1=m5.id5; 

表mytable的是巨大的,但我需要從它那裏得到的所有信息,我需要得到多次使用子查詢,問題是:如何保存結果查詢(SELECT * FROM mytable):

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test 
FROM 
(SELECT * FROM mytable) AS m1, 
m1 AS m2, 
m1 AS m3, 
m1 AS m4, 
m1 AS m5 
WHERE m1.id1=m2.id2 AND m1.id1=m3.id3 AND m1.id1=m4.id4 AND m1.id1=m5.id5; 

非常感謝。

回答

1

你應該使用JOIN

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test 
FROM mytable m1 
JOIN mytable m2 ON m1.id1 = m2.id2 
JOIN mytable m3 ON m1.id1 = m3.id3 
JOIN mytable m4 ON m1.id1 = m4.id4 
JOIN mytable m5 ON m1.id1 = m5.id5 
+0

你是科羅拉多州最快的一個! –

1

正確的方法是使用JOIN並且讓優化器做他的工作:

SELECT m1.id1,m2.user,m3.pass,m4.cap,m5.test 
    FROM mytable as m1 
    JOIN mytable as m2 ON m1.id1=m2.id2 
    JOIN mytable as m3 ON m1.id1=m3.id3 
    JOIN mytable as m4 ON m1.id1=m4.id4 
    JOIN mytable as m5 ON m1.id1=m5.id5 ; 

它沒有必要使用子查詢選擇所有來自一張桌子的數據......

相關問題