我有一个连接几个表的查询,我试图使用case语句创建几个列来确定不能按预期工作的值(超链接文本)。我想查看连接产生的行,如果其中一个左连接的值不为null,那么我想将一些文本与值连接起来,如果它是null,我将用不同的值连接文本。查询是:
SELECT p.*, p.id AS ppe_id, CONCAT(u.last_name, ',', u.first_name)
AS name, sub1.created_at AS last_inspection,
CASE
WHEN (sub1.created_at IS NULL AND man_date < now() - interval '1 year' )
THEN 'over-due'
WHEN (sub1.created_at IS NULL AND man_date < now() - interval '11 months' )
THEN 'due'
WHEN sub1.created_at < now() - interval '11 months'
THEN 'due'
WHEN sub1.created_at < now() - interval '1 year'
THEN 'over-due'
ELSE 'none'
END
AS inspection_due,
CASE
WHEN now() > p.man_date + interval '10 years'
THEN 'over-due'
WHEN now() > p.man_date + interval '119 months'
THEN 'due'
ELSE 'none'
END
AS expiring,
CASE
WHEN cl_sub.ppe_id IS NOT NULL
THEN '<a class="btn btn-xs btn-back" href="/cleanings/' || cl_sub.id || '">Cleaning</a>'
WHEN cl_sub.ppe_id IS NULL
THEN '<a class="btn btn-xs btn-success" href="/ppes/'|| cl_sub.ppe_id || '/cleanings/new">Clean</a>'
END
AS cleaning_button,
CASE
WHEN r_sub.ppe_id IS NOT NULL
THEN '<a class="btn btn-xs btn-back" href="/repairs/' || r_sub.id || '">Repairing</a>'
WHEN r_sub.ppe_id IS NULL
THEN'<a class="btn btn-xs btn-success" href="/ppes/' || r_sub.ppe_id || '/repairs/new">Repair</a>'
END
AS repair_button
FROM ppes p
JOIN users u
ON p.user_id = u.id
LEFT OUTER JOIN (SELECT DISTINCT ppe_id, id FROM cleanings cl WHERE cleaning_date IS NULL ) AS cl_sub
ON p.id = cl_sub.ppe_id
LEFT OUTER JOIN (SELECT DISTINCT ppe_id, id FROM repairs r WHERE completed IS NULL ) AS r_sub
ON p.id = r_sub.ppe_id
LEFT OUTER JOIN (SELECT t.ppe_id, t.created_at
FROM inspections t
INNER JOIN (SELECT ppe_id, max(created_at) as LastInsp
FROM inspections
GROUP BY ppe_id ) tm
ON t.ppe_id = tm.ppe_id AND t.created_at = tm.LastInsp
WHERE (t.advanced = true AND t.passed = true) order by t.ppe_id) AS sub1
ON sub1.ppe_id = p.id;
问题在于,即使两个连接产生了预期的效果,即它们要么为子查询中的任何一个提出id,要么得出null,我只从每个case语句的前半部分得到输出:
CASE
WHEN cl_sub.ppe_id IS NOT NULL
THEN '<a class="btn btn-xs btn-back" href="/cleanings/' || cl_sub.id || '">Cleaning</a>'
WHEN cl_sub.ppe_id IS NULL
THEN '<a class="btn btn-xs btn-success" href="/ppes/'|| cl_sub.ppe_id || '/cleanings/new">Clean</a>'
END
AS cleaning_button,
CASE
WHEN r_sub.ppe_id IS NOT NULL
THEN '<a class="btn btn-xs btn-back" href="/repairs/' || r_sub.id || '">Repairing</a>'
WHEN r_sub.ppe_id IS NULL
THEN'<a class="btn btn-xs btn-success" href="/ppes/' || r_sub.ppe_id || '/repairs/new">Repair</a>'
END
AS repair_button
如果我在表中查询这些字段,它会显示清理表或修复表在连接中返回ID的情况,它会输入所需的值,但是否则会将其留空而不是放入第二个案例陈述中的价值:
ppe_development=# select id, cleaning_button, repair_button from test_view_ppes order by cleaning_button, repair_button;
id | cleaning_button | repair_button
-----+------------------------------------------------------------------+-----------------------------------------------------------------
126 | <a class="btn btn-xs btn-back" href="/cleanings/13">Cleaning</a> |
13 | <a class="btn btn-xs btn-back" href="/cleanings/24">Cleaning</a> |
115 | <a class="btn btn-xs btn-back" href="/cleanings/27">Cleaning</a> |
113 | <a class="btn btn-xs btn-back" href="/cleanings/5">Cleaning</a> | <a class="btn btn-xs btn-back" href="/repairs/3">Repairing</a>
114 | <a class="btn btn-xs btn-back" href="/cleanings/6">Cleaning</a> | <a class="btn btn-xs btn-back" href="/repairs/14">Repairing</a>
53 | <a class="btn btn-xs btn-back" href="/cleanings/8">Cleaning</a> |
1 | <a class="btn btn-xs btn-back" href="/cleanings/9">Cleaning</a> | <a class="btn btn-xs btn-back" href="/repairs/13">Repairing</a>
54 | | <a class="btn btn-xs btn-back" href="/repairs/16">Repairing</a>
57 | | <a class="btn btn-xs btn-back" href="/repairs/17">Repairing</a>
56 | | <a class="btn btn-xs btn-back" href="/repairs/22">Repairing</a>
58 | | <a class="btn btn-xs btn-back" href="/repairs/25">Repairing</a>
118 | | <a class="btn btn-xs btn-back" href="/repairs/26">Repairing</a>
7 | | <a class="btn btn-xs btn-back" href="/repairs/28">Repairing</a>
15 | | <a class="btn btn-xs btn-back" href="/repairs/30">Repairing</a>
11 | | <a class="btn btn-xs btn-back" href="/repairs/32">Repairing</a>
120 | | <a class="btn btn-xs btn-back" href="/repairs/33">Repairing</a>
123 | | <a class="btn btn-xs btn-back" href="/repairs/5">Repairing</a>
116 | | <a class="btn btn-xs btn-back" href="/repairs/7">Repairing</a>
78 | |
ppes table:
id | serial | man_date | category | created_at | updated_at | user_id | note | size | manufacturer | active | cost | cbrn_rated
---+--------+------------+----------+----------------------------+----------------------------+---------+------+------+--------------+--------+------+------------
12 | 10007 | 2017-01-25 | jackets | 2017-01-25 17:40:10.652715 | 2017-04-11 00:46:58.154629 | 5 | | | | t | 0.00 |
18 | 10013 | 2017-01-25 | jackets | 2017-01-25 17:40:10.663324 | 2017-04-11 00:46:58.187638 | 6 | | | | t | 0.00 |
20 | 10015 | 2017-01-25 | gloves | 2017-01-25 17:40:10.66659 | 2017-04-11 00:46:58.199712 | 6 | | | | t | 0.00 |
24 | 10019 | 2017-01-25 | jackets | 2017-01-25 17:40:10.673236 | 2017-04-11 00:46:58.228069 | 8 | | | | t | 0.00 |
26 | 10021 | 2017-01-25 | gloves | 2017-01-25 17:40:10.67665 | 2017-04-11 00:46:58.239997 | 8 | | | | t | 0.00 |
清洁表:
id | cleaning_date | user_id | ppe_id | notes | created_at | updated_at | failed_inspection
---+---------------+---------+--------+----------------------------+----------------------------+----------------------------+-------------------
5 | | 1 | 113 | 1 | 2017-04-01 21:11:28.882502 | 2017-04-01 21:11:28.882502 | f
6 | | 1 | 114 | 2 | 2017-04-01 21:11:41.068899 | 2017-04-01 21:11:41.068899 | f
8 | | 1 | 53 | Bamboo | 2017-04-07 19:36:17.48159 | 2017-04-07 19:36:17.48159 | f
13 | | 1 | 126 | testing new routing | 2017-06-25 20:38:16.813986 | 2017-06-25 20:38:16.813986 | f
维修表:
id | order_date | completed | user_id | ppe_id | notes | created_at | updated_at | failed_inspection | cost
---+------------+-----------+---------+--------+----------------------+----------------------------+----------------------------+-------------------+------
5 | | | 1 | 123 | testing photo upload | 2017-04-03 19:20:37.175447 | 2017-04-03 19:20:37.175447 | f | 0.00
7 | | | 1 | 116 | testing large photo. | 2017-04-03 19:56:12.391366 | 2017-04-03 19:56:12.391366 | f | 0.00
13 | | | 1 | 1 | acl test | 2017-04-04 07:35:59.974909 | 2017-04-04 07:35:59.974909 | f | 0.00
14 | | | 1 | 114 | rover1 upload\r +| 2017-04-04 07:57:16.602674 | 2017-04-04 07:58:50.505527 | f | 0.00
16 | | | 1 | 54 | | 2017-04-04 08:53:29.394382 | 2017-04-04 08:53:29.394382 | f | 0.00
id | passed | user_id | ppe_id | created_at | advanced
---+--------+---------+--------+----------------------------+----------
22 | f | 7 | 6 | 2017-06-20 17:39:37.253423 | f
34 | f | 1 | 4 | 2017-06-22 20:07:24.214546 | f
23 | f | 9 | 20 | 2017-06-20 20:00:02.506964 | f
26 | t | 5 | 42 | 2017-06-20 20:07:23.207904 | f
24 | t | 10 | 4 | 2017-06-20 20:01:28.161158 | f
答案 0 :(得分:1)
如果r_sub.ppe_id IS NULL
,那么连接也将成为NULL
......
因为 SQL 中的几乎所有函数和操作都返回NULL
,如果它的任何参数是NULL
。 (NULL解释为“未知值”...)