gpt4 book ai didi

mysql - 当值为 NULL 时删除 SQL 输出

转载 作者:行者123 更新时间:2023-12-01 00:52:08 25 4
gpt4 key购买 nike

下面的 SQL 查询对我有用,但是当 staffNo 为 null 6 次(每个员工一次)时,它会返回属性值。即使staffNo为null,我也想返回属性(property)所有者,没有返回6。

SELECT s.branchNo, s.staffNo, s.fName, s.lName, propertyNo, p.staffNo,
o.fName, o.lName, o.ownerNo
FROM staff s, propertyforrent p, privateowner o
WHERE s.staffNo = p.staffNo AND o.ownerNo = p.ownerNo
OR p.staffNo IS NULL AND o.ownerNo = p.ownerNo
ORDER BY s.branchNo, s.staffNo, propertyNo

结果

B003    SG14    David   Ford    PG16    SG14    Tony    Shaw    CO93
B003 SG14 David Ford PG4 NULL Tina Murphy CO40
B003 SG37 Ann Beech PG21 SG37 Carol Farrel CO87
B003 SG37 Ann Beech PG36 SG37 Tony Shaw CO93
B003 SG37 Ann Beech PG4 NULL Tina Murphy CO40
B003 SG5 Susan Brand PG4 NULL Tina Murphy CO40
B005 SL21 John White PG4 NULL Tina Murphy CO40
B005 SL41 Julie Lee PG4 NULL Tina Murphy CO40
B005 SL41 Julie Lee PL94 SL41 Carol Farrel CO87
B007 SA9 Mary Howe PA14 SA9 Joe Keogh CO46
B007 SA9 Mary Howe PG4 NULL Tina Murphy CO40

这是想要的结果

B003    SG14    David   Ford    PG16    SG14    Tony    Shaw    CO93
B003 SG14 PG4 NULL Tina Murphy CO40
B003 SG37 Ann Beech PG21 SG37 Carol Farrel CO87
B003 SG37 Ann Beech PG36 SG37 Tony Shaw CO93
B005 SL41 Julie Lee PL94 SL41 Carol Farrel CO87
B007 SA9 Mary Howe PA14 SA9 Joe Keogh CO46

提前致谢!

最佳答案

使用左连接可能会有帮助

SELECT s.branchNo, s.staffNo, s.fName, s.lName, propertyNo, p.staffNo,
o.fName, o.lName, o.ownerNo
FROM privateowner o
LEFT JOIN propertyforrent p ON o.ownerNo = p.ownerNo
LEFT JOIN staff s ON s.staffNo = p.staffNo

ORDER BY s.branchNo, s.staffNo, propertyNo

关于mysql - 当值为 NULL 时删除 SQL 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com