2014-10-20 135 views
0

我在數據庫中有20個表。MySQL從多個表中獲取結果

我所試圖做的就是誰擁有得分所有學生的名單高於70

我的查詢:

SELECT mysql.*, mssql.*, orecle.* FROM mysql, mssql, orecle; 

+----------+------------+------------+------------+----------+------------+------------+------------+----------+-------------+-------------+-------------+ 
| student | mysqltest1 | mysqltest2 | mysqltest3 | student | mssqltest1 | mssqltest2 | mssqltest3 | student | orecletest1 | orecletest2 | orecletest3 | 
+----------+------------+------------+------------+----------+------------+------------+------------+----------+-------------+-------------+-------------+ 
| student1 |   60 |   70 |   80 | student1 |   80 |   60 |   70 | student1 |   80 |   60 |   50 | 
| student2 |   50 |   80 |   90 | student1 |   80 |   60 |   70 | student1 |   80 |   60 |   50 | 
| student1 |   60 |   70 |   80 | student2 |   90 |   70 |   50 | student1 |   80 |   60 |   50 | 
| student2 |   50 |   80 |   90 | student2 |   90 |   70 |   50 | student1 |   80 |   60 |   50 | 
| student1 |   60 |   70 |   80 | student1 |   80 |   60 |   70 | student2 |   90 |   70 |   80 | 
| student2 |   50 |   80 |   90 | student1 |   80 |   60 |   70 | student2 |   90 |   70 |   80 | 
| student1 |   60 |   70 |   80 | student2 |   90 |   70 |   50 | student2 |   90 |   70 |   80 | 
| student2 |   50 |   80 |   90 | student2 |   90 |   70 |   50 | student2 |   90 |   70 |   80 | 
+----------+------------+------------+------------+----------+------------+------------+------------+----------+-------------+-------------+-------------+ 
8 rows in set (0.00 sec) 

爲什麼它顯示8行,而不是2?

我希望發生的是當查詢運行,只輸出有一個值高於70

我厭倦了一對夫婦查詢但是似乎沒有任何工作,他們都給了我錯誤的細胞。

SELECT mysql.*, mssql.*, orecle.* FROM mysql, mssql, orecle where ALL > 70; 
SELECT mysql.*, mssql.*, orecle.* FROM mysql, mssql, orecle where ALL > '70'; 
SELECT mysql.*, mssql.*, orecle.* FROM mysql, mssql, orecle where mysql.* OR mssql.* OR orecle.* > 70; 
SELECT mysql.*, mssql.*, orecle.* FROM mysql, mssql, orecle where mysql.* OR mssql.* OR orecle.* > '70'; 

該查詢會給我一個行,我認爲是更好的

SELECT mysql.*, mssql.*, orecle.* FROM mysql, mssql, orecle where mysql.student='student1' AND mssql.student='student1' AND orecle.student='student1'; 


+----------+------------+------------+------------+----------+------------+------------+------------+----------+-------------+-------------+-------------+ 
| student | mysqltest1 | mysqltest2 | mysqltest3 | student | mssqltest1 | mssqltest2 | mssqltest3 | student | orecletest1 | orecletest2 | orecletest3 | 
+----------+------------+------------+------------+----------+------------+------------+------------+----------+-------------+-------------+-------------+ 
| student1 |   60 |   70 |   80 | student1 |   80 |   60 |   70 | student1 |   80 |   60 |   50 | 
+----------+------------+------------+------------+----------+------------+------------+------------+----------+-------------+-------------+-------------+ 
1 row in set (0.01 sec) 

然而,可以讓複雜的〜20桌; 此外,我只想看到值70或更高的結果。

這會發生什麼最好的查詢?

+0

聯盟聲明? – fyr 2014-10-20 21:05:25

+1

'JOIN's和'INDEX'es – doublesharp 2014-10-20 21:06:53

回答

4

嘗試:

SELECT * FROM mysql INNER JOIN 
mssql USING (student) INNER JOIN 
orecle USING (student); 

爲了得到兩行,而不是2×2×2 = 8行。

要隱藏細胞得分低於70,請嘗試:

SELECT IF(mysqltest1>70,mysqltest1,null) AS M1, 
IF(mysqltest2>70,mysqltest2,null) AS M2, 
IF(mysqltest3>70,mysqltest3,null) AS M3, 
IF(mssqltest1>70,mssqltest1,null) AS S1, 
IF(mssqltest2>70,mssqltest2,null) AS S2, 
IF(mssqltest3>70,mssqltest3,null) AS S3, 
IF(orecletest1>70,orecletest1,NULL) as O1, 
IF(orecletest2>70,orecletest2,NULL) as O2, 
IF(orecletest3>70,orecletest3,NULL) as O3 FROM 
mysql INNER JOIN 
mssql USING (student) INNER JOIN 
orecle USING (student); 

NB:你有在Oracle中的錯字。