我在数据库中有大约20个表。
我要做的是获得所有分数高于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 :(得分:4)
尝试:
SELECT * FROM mysql INNER JOIN
mssql USING (student) INNER JOIN
orecle USING (student);
为了获得两行,而不是2 x 2 x 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);
注意:您在Oracle中输了一个错字。