简体   繁体   English

MySQL:当一个表上存在多个连接时,计算NULL或0值

[英]MySQL: Accounting for NULL or 0 values when multiple joins exist on one table

I have a query that looks up people's full names based on their record ID's in a table called users . 我有一个查询,根据他们在名为users的表中的记录ID查找人的全名。 The full names are tied to their roles in another table ( table1 ). 全名与另一个表( table1 )中的角色相关联。 This requires multiple joins to the users table: 这需要多次连接到users表:

SELECT table1.id, users.full_name AS "Requester", 
users.full_name AS "Approver," 
users.full_name AS "Ordered By", 
users.full_name AS "Received By" 
FROM table1
JOIN users AS users
ON table1.requester_id = users.id
JOIN users AS users2
ON table1.approver_id = users2.id
JOIN users AS users3 
ON table1.ordered_by = users3.id
JOIN users AS users4
ON table1.received_by = users4.id
WHERE table1.deleted_record !=1;

The problem I'm having is with ordered_by and received_by . 我遇到的问题是ordered_byreceived_by Often, they don't yet exist, because the order has neither been ordered nor received, so the ID for each can be 0, which has no corresponding value in the users table. 通常,它们尚不存在,因为订单既没有被订购也没有收到,因此每个订单的ID可以是0,这在users表中没有相应的值。 When I run this query, I should get back all 475 records that exist, but I only get back 365, because of those 0 values. 当我运行这个查询时,我应该找回所有存在的475条记录,但由于这些0值,我只能返回365条。 How can I modify this query to make sure all rows are returned, even if ordered_by and/or received_by = 0? 如何修改此查询以确保返回所有行,即使ordered_by和/或received_by = 0?

First, your primary table driving the query should be table1 . 首先,驱动查询的主表应该是table1 Then, you are using JOIN instead of LEFT JOIN . 然后,您正在使用JOIN而不是LEFT JOIN LEFT JOIN will give you a null result if no link, but not fail. 如果没有链接, LEFT JOIN会给你一个null结果,但不会失败。 In which case, you might have to use an IF for your fields value 在这种情况下,您可能必须为字段值使用IF

SELECT table1.id, req.full_name AS "Requester", 
   app.full_name AS "Approver", 
   ordr.full_name AS "Ordered By", 
   rec.full_name AS "Received By" 
FROM table1
LEFT JOIN users AS req
   ON table1.requester_id = req.id
LEFT JOIN users AS app
   ON table1.approver_id = app.id
LEFT JOIN users AS ordr
   ON table1.ordered_by = ordr.id
LEFT JOIN users AS rec
   ON table1.received_by = rec.id
WHERE table1.deleted_record !=1;

This should do it 这应该做到这一点

You are looking for left join : 您正在寻找left join

SELECT t1.id, ur.full_name AS "Requester", 
       ua.full_name AS "Approver," 
       uo.full_name AS "Ordered By", 
       urv.uo AS "Received By" 
FROM table1 t1 LEFT JOIN
     users ur
     ON t1.requester_id = ur.id LEFT JOIN
     users ua
     ON t1.approver_id = ua.id LEFT JOIN
     users uo 
     ON t1.ordered_by = uo.id LEFT JOIN
     users urv
     ON t1.received_by = urv.id
WHERE t1.deleted_record <> 1;

Note that I changed the aliases on the users references from fairly meaningless u1 , u2 , etc. to ua , uo , and so on. 请注意,我将users引用上的别名从相当无意义的u1u2等更改为uauo等。 Also, these need to be used in the SELECT to get the right full name. 此外,这些需要在SELECT以获得正确的全名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM