SQL INNER JOIN多个表无法按预期工作 [英] SQL INNER JOIN multiple tables not working as expected
问题描述
我试图更好地理解SQL数据库中的JOIN或INNER JOIN多个表。
I'm trying to have a better understanding of JOIN or INNER JOIN multiple tables in a SQL database.
这就是我所拥有的:
SQL查询:
SELECT *
FROM csCIDPull
INNER JOIN CustomerData ON CustomerData.CustomerID = csCIDPull.CustomerID
INNER JOIN EMSData ON EMSData.EmsID = csCIDPull.EmsID
;
如果删除 INNER JOIN EMSData $,则不会返回任何结果c $ c>部分,它提供
CustomerData
和 csCIDPull
表中的信息。我的思维方式可能不正确。我假设有5个表都带有一个int ID,这些ID还将提交到一个表中以合并所有表(MAIN表仅包含ID,而其他表包含数据)。
This returns NO results, if I remove the INNER JOIN EMSData
section, it provides the info from CustomerData
and csCIDPull
tables. My method of thinking may be incorrect. I have let's say 5 tables all with a int ID, those ID's are also submitting to a single table to combine all tables (the MAIN table contains only ID's while the other tables contain the data).
想通了,我会向你们射击,看看我可能做错了什么。 -谢谢
Figured I'd shoot you folks posting to see what I might be doing wrong. -Thanks
推荐答案
基本上,听起来您的EMSData表中没有匹配的数据。您需要为此使用外部联接
:
Basically it sounds like you don't have matching data in your EMSData table. You would need to use an OUTER JOIN
for this:
SELECT *
FROM csusaCIDPull
LEFT JOIN CustomerData ON CustomerData.CustomerID = csCIDPull.CustomerID
LEFT JOIN EMSData ON EMSData.EmsID = csCIDPull.EmsID
侧面说明:考虑不返回 *
而是选择每个表格中您想要的字段。
Side note: consider not returning *
but rather select the fields you want from each table.
这篇关于SQL INNER JOIN多个表无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!