如何修复此 SQL GROUP BY 查询?
我有以下查询:
SELECT
dev.DeviceName, Count(dom.DomainID) AS CountOfDomains
FROM
tblDevices dev
JOIN
tblIPNumbers ip ON dev.DeviceName = ip.ServerName
JOIN
tblDomains dom ON dom.IPNumberID = ip.IPNumberID
WHERE
dom.PointerTo=0
AND dev.DeviceType='3'
AND (dev.[System]='32' OR dev.[System]='33')
AND dom.ClosedDate IS NULL AND dev.Active=1
GROUP BY
dev.DeviceName
ORDER BY
Count(dom.DomainID)
表格如下所示:
tblDomains ========== DomainID int IPNumberID int ClosedDate datetime PointerTo int tblIPNumbers ============ IPNumberID int ServerName varchar(200) tblDevices ========== DeviceID int DeviceName varchar(200) System varchar(10) DeviceType varchar(10) Active bit
示例数据:
tblDomains: =========== DomainID: 1234 IPNumberID: 1000 ClosedDate: NULL PointerTo: 0 tblIPNumbers: ============= IPNumberID: 1000 ServerName: WIN2008-01 tblDevices: =========== DeviceID: 1 DeviceName: WIN2008-01 System: 32 Active: 1 DeviceType: 3
问题是,如果 tblDomains
中没有与 tblIPNumbers
中的 IPNumberID
匹配的行code> 我没有返回任何行。在这种情况下,我希望查询为 Count(dom.DomainID) AS CountOfDomains
返回一行 0
。
我尝试过 LEFT 和 RIGHT 连接的各种组合,这似乎是一个简单的问题,但今天我的 SQL-fu 很低。
I have the following query:
SELECT
dev.DeviceName, Count(dom.DomainID) AS CountOfDomains
FROM
tblDevices dev
JOIN
tblIPNumbers ip ON dev.DeviceName = ip.ServerName
JOIN
tblDomains dom ON dom.IPNumberID = ip.IPNumberID
WHERE
dom.PointerTo=0
AND dev.DeviceType='3'
AND (dev.[System]='32' OR dev.[System]='33')
AND dom.ClosedDate IS NULL AND dev.Active=1
GROUP BY
dev.DeviceName
ORDER BY
Count(dom.DomainID)
The tables look like:
tblDomains ========== DomainID int IPNumberID int ClosedDate datetime PointerTo int tblIPNumbers ============ IPNumberID int ServerName varchar(200) tblDevices ========== DeviceID int DeviceName varchar(200) System varchar(10) DeviceType varchar(10) Active bit
Sample Data:
tblDomains: =========== DomainID: 1234 IPNumberID: 1000 ClosedDate: NULL PointerTo: 0 tblIPNumbers: ============= IPNumberID: 1000 ServerName: WIN2008-01 tblDevices: =========== DeviceID: 1 DeviceName: WIN2008-01 System: 32 Active: 1 DeviceType: 3
The problem is that if there are no rows in tblDomains
that match an IPNumberID
in tblIPNumbers
I get no rows returned. I'd like the query to return a single row of 0
for Count(dom.DomainID) AS CountOfDomains
in this case.
I've tried various combinations of LEFT
and RIGHT
joins and it seems like a simple problem but my SQL-fu is low today.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
LEFT JOIN
而不是JOIN
,并且还要注意,当LEFT JOIN
在右侧找不到匹配的行时表,它将为该右表中的任何字段返回 NULL。这意味着您的问题来自您的
WHERE
子句,该子句以非NULL
安全的方式引用dom.pointerto
,这将有效地消除好处LEFT JOIN
的。You should use
LEFT JOIN
s instead ofJOIN
s, and also beware that when aLEFT JOIN
doesn't find a matching row in the right table, it'll returnNULL
s for any field from that right table.That means you issue comes from your
WHERE
clause that referencesdom.pointerto
in a non-NULL
-safe way, which will effectively void the benefits of theLEFT JOIN
.将您的
JOIN
更改为LEFT JOIN
,它们也会包含不匹配的记录。Change your
JOIN
s toLEFT JOIN
and they'll include non-matching records as well.尝试对表 tbldomains 使用外连接
Try Using outer join for table tbldomains