我有以下三个表格,因为在这三个表格中没有唯一的键,我无法加入 计算机,客户和计算机驱动器
*Table Computers*
ComputerID ClientID Name Domain UserName OS
========================================================================
1 1 WS01 ABC Admin 7
2 1 WS02 ABC Admin 7
3 1 WS03 ABC Admin 7
4 2 CH21 CORP Admin 7
5 2 CH22 CORP Admin 7
6 3 LT33 WGE Admin 7
*Table Clients*
ClientID Client Name
=======================
1 Company1
2 Company2
3 Company3
*Table Drives*
DriveID ComputerID Letter Size Free Missing FileSystem
======================================================================
1 1 C 102400 100000 0 NTFS
2 1 D 102400 100000 0 NTFS
3 1 E 102400 100000 1 FAT32
4 2 C 102400 100000 0 NTFS
5 3 C 102400 100000 0 NTFS
6 4 C 102400 100000 0 NTFS
7 4 F 102400 100000 0 NTFS
8 4 E 102400 100000 1 FAT32
9 4 D 102400 100000 0 NTFS
10 5 C 102400 100000 0 NTFS
11 5 D 102400 100000 0 NTFS
12 6 C 102400 100000 0 NTFS
我有以下查询:
SELECT cl.Name, comp.Name, dr.`Letter`, dr.`Free`,dr.`Size`,dr.`FileSystem`,
dr.`Missing`
FROM clients AS cl
INNER JOIN computers AS comp ON comp.`ClientID` = cl.clientid
LEFT OUTER JOIN drives AS dr ON dr.`ComputerID` = comp.`ComputerID`
我期待以下结果
Expected Result:
ClientName ComputerName Letter Free Size Filesystem Missing
=======================================================================
Company1 WS01 C 100000 102400 NTFS 0
Company1 WS01 D 100000 102400 NTFS 0
Company1 WS01 E 100000 102400 NTFS 1
Company1 WS02 C 100000 102400 NTFS 0
Company1 WS03 C 100000 102400 NTFS 0
Company2 CH21 C 100000 102400 NTFS 0
Company2 CH21 F 100000 102400 NTFS 0
Company2 CH21 E 100000 102400 NTFS 1
Company2 CH21 D 100000 102400 NTFS 0
Company2 CH22 C 100000 102400 NTFS 0
Company2 CH22 D 100000 102400 NTFS 0
Company3 LT33 C 100000 102400 NTFS 0
当我运行上面显示的查询时,有人可以解释为什么我缺少行吗?我没有得到预期的结果。当所有三个表之间没有唯一键时,它们是否也可以提供一种正确连接3个表的方法
答案 0 :(得分:1)
您的代码看起来很稳固。我唯一能想到的可能是你的JOINS顺序。
这里有关于加入订单的好文章。
答案 1 :(得分:0)
SELECT cl.NAME, comp.NAME, dr.`Letter`, dr.`Free`, dr.`Size`, dr.`FileSystem`, dr.`Missing`
FROM Computers AS comp
INNER JOIN Clients AS cl ON comp.ClientID = cl.ClientID
LEFT OUTER JOIN Drives AS dr ON dr.ComputerID = comp.ComputerID
缺少行是什么意思?
答案 2 :(得分:0)