我有4个表,有不同类型的数据。
订单(StudentId,StoreId,AdvisorId,日期)
学生(StudentId,FirstName,姓氏,地址)
商店(StoreId,StoreNAme,StoreAddress)
Advisors(AdvisorId,AdvisorName,AdvisorAddress)
我的订单表有1400万条记录。我想做的就是得到 订单。*,Student.FirstName,Student.LastName,Student.Address,Stores.StoreNAme,Stores,StoreAddress,Advisors.AdvisorNAme,Advisors.AdvisorAddress
使用下面的LEFT JOIN查询。我相信结果应该只包含1400万条记录,如订单。但我得到的更多是1400万。
有人可以告诉我们这个查询有什么问题吗
Select Orders.*, Student.FirstName, Student.LastName, Student.Address, Stores.StoreNAme, Stores,StoreAddress, Advisors.AdvisorNAme, Advisors.AdvisorAddress from Orders
LEFT JOIN Students on Orders.StudentId = Students.StudentId
LEFT JOIN Stores on Orders.StoreID = Stores.StoreId
LEFT JOIN Advisors on Orders.AdvisorId = Advisors.AdvisorId
Where Orders.Date > 'Jan102014'
任何帮助都将不胜感激。
答案 0 :(得分:0)
阅读您的问题,看起来您想要每个订单的学生列表,商店。所以你应该使用内部联接与订单而不是左联接。
Select
Orders . *,
Student.FirstName,
Student.LastName,
Student.Address,
Stores.StoreNAme,
Stores,
StoreAddress,
Advisors.AdvisorNAme,
Advisors.AdvisorAddress
from
Orders
INNER JOIN
Students ON Orders.StudentId = Students.StudentId
INNER JOIN
Stores ON Orders.StoreID = Stores.StoreId
INNER JOIN
Advisors ON Orders.AdvisorId = Advisors.AdvisorId
Where
Orders.Date > 'Jan102014'