如何从SQL中的三个表中检索数据 [英] How to retrieve data from three tables in SQL
本文介绍了如何从SQL中的三个表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这张桌子
table1 table2 table3
--------------- ---------------------------- ---------------------
NO. courses NO. id courses grade NO. courses
--------------- ----------------------------- ---------------------
1 CHEM 101 1 2255 CHEM 101 A+ 2 English
2 ENGL 101 2 2255 English A
3 MATH 101
我如何编写 SQL 查询以从以前的表中按 id 检索数据,以便给我这样的表,其中它们之间的关系为 NO.
How I can write SQL query to retrieve data by id from previous tables in order to give me like this table where the relation between them is NO.
----------------------------------------
course grade equal of my courses
----------------------------------------
CHEM 101 A+ null
ENGL 101 A English
MATH 101 null null
推荐答案
你应该使用 LEFT JOIN 用于.我假设 NO 是维护所有表之间关系的列名
You should use LEFT JOIN for. I assume NO is a column name that maintain the relationship between all tables
SELECT t1.courses,t2.grade,t3.courses AS 'equal of my courses'
FROM table1 t1
LEFT JOIN table2 t2
ON t1.NO = t2.NO
LEFT JOIN table3 t3
ON t2.NO = t3.NO;
这篇关于如何从SQL中的三个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文