简体   繁体   中英

MySQL Get results from multiple tables

I have ~20 tables in a database.

What i am trying to do is get a list of all students who have a score higher that 70

My Query:

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)

Why does it show 8 rows and not 2?

What i want to happen is when the query runs to only output the cells that have a value higher that 70.

I tired a couple queries however nothing seems to work they all gave me errors.

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';

This query will give me one row which i think is better

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)

However that can get complex for ~20 tables; Also i only want to see results for values 70 or higher.

What would be the best query to get this to happen?

Try:

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

In order to get two rows, rather than 2 x 2 x 2 = 8 rows.

To hide cells with scores below 70, try:

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: you've got a typo in Oracle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM