gpt4 book ai didi

mysql - 将空值分配给查询中重复的匹配行

转载 作者:行者123 更新时间:2023-11-29 11:01:05 25 4
gpt4 key购买 nike

我是 SQL 新手

我有以下查询,

select v.pattern_id,v.id,v.pdb_id as viocardId,p.pdb_id as PID,s.vio_id as SiVioId, r.date as orcdate, s.created_date as sivcreateddate
from orc_orcviolation v, orc_orcpattern p, siverify_siverifyverificationsite s, orc_orccheck o, orc_orcrecord r
where v.pattern_id = p.id
and p.pdb_id = s.pattern_id
and p.if_target = s.if_target
and p.if_secure <> 1
and v.check_id = o.id
and o.record_id = r.id

我得到上述查询的以下结果,

pattern_id  id  viocardId   PID SiVioId orcdate sivcreateddate
469064 1205777 3024788 2335310 1739560 2014-05-02 16:14:32 2015-06-21 13:18:49
469064 1241653 3097963 2335310 1739560 2014-05-24 17:07:55 2015-06-21 13:18:49
469064 1309143 3393980 2335310 1739560 2014-07-19 17:49:11 2015-06-21 13:18:49
469064 1514825 4079963 2335310 1739560 2014-10-29 14:07:58 2015-06-21 13:18:49
469064 1516474 4085403 2335310 1739560 2014-10-30 20:52:10 2015-06-21 13:18:49
469064 1613816 4692514 2335310 1739560 2015-01-10 12:29:30 2015-06-21 13:18:49
469064 1631484 4728047 2335310 1739560 2015-01-23 19:49:30 2015-06-21 13:18:49
469064 1700479 5413265 2335310 1739560 2015-04-16 15:01:08 2015-06-21 13:18:49
469064 1702863 5416425 2335310 1739560 2015-04-22 00:07:01 2015-06-21 13:18:49
**469064 1739560 5540728 2335310 1739560 2015-06-17 05:12:05 2015-06-21 13:18:49**
469064 1911207 5818507 2335310 1739560 2015-12-04 07:53:33 2015-06-21 13:18:49
469064 1940132 5828037 2335310 1739560 2015-12-19 03:24:25 2015-06-21 13:18:49

在 ** 标记的行中,v.id 和 s.vio_id 的值是匹配的,除了匹配行之外,是否有任何方法可以让查询为 sivioId 列的其他行返回 null?

最佳答案

您正在寻找的是CASE声明。

您可以根据结果行中的值选择要返回的值。在这种情况下,当 s.vio_id!=v.id 时,您将返回 null

SELECT v.pattern_id, v.id, v.pdb_id AS viocardId, p.pdb_id AS PID, 
CASE WHEN s.vio_id=v.id THEN s.vio_id ELSE null END AS SiVioId,
r.date AS orcdate, s.created_date AS sivcreateddate

FROM orc_orcviolation AS v
LEFT JOIN orc_orcpattern AS p ON v.pattern_id = p.id
LEFT JOIN siverify_siverifyverificationsite s
ON p.pdb_id = s.pattern_id and p.if_target = s.if_target
LEFT JOIN orc_orccheck AS o ON v.check_id = o.id
LEFT JOIN orc_orcrecord AS r ON o.record_id = r.id
WHERE p.if_secure <> 1

您的问题的答案来 self 的查询的 SELECT 子句。
我使用 JOINs 重写了您的查询因为我发现更容易看到表格之间的联系。如果您不想使用联接,请随意仅使用答案的 SELECT 部分。

此查询应产生:

pattern_id id      viocardId PID     SiVioId orcdate             sivcreateddate
469064 1205777 3024788 2335310 null 2014-05-02 16:14:32 2015-06-21 13:18:49
469064 1241653 3097963 2335310 null 2014-05-24 17:07:55 2015-06-21 13:18:49
469064 1309143 3393980 2335310 null 2014-07-19 17:49:11 2015-06-21 13:18:49
469064 1514825 4079963 2335310 null 2014-10-29 14:07:58 2015-06-21 13:18:49
469064 1516474 4085403 2335310 null 2014-10-30 20:52:10 2015-06-21 13:18:49
469064 1613816 4692514 2335310 null 2015-01-10 12:29:30 2015-06-21 13:18:49
469064 1631484 4728047 2335310 null 2015-01-23 19:49:30 2015-06-21 13:18:49
469064 1700479 5413265 2335310 null 2015-04-16 15:01:08 2015-06-21 13:18:49
469064 1702863 5416425 2335310 null 2015-04-22 00:07:01 2015-06-21 13:18:49
469064 1739560 5540728 2335310 1739560 2015-06-17 05:12:05 2015-06-21 13:18:49
469064 1911207 5818507 2335310 null 2015-12-04 07:53:33 2015-06-21 13:18:49
469064 1940132 5828037 2335310 null 2015-12-19 03:24:25 2015-06-21 13:18:49

关于mysql - 将空值分配给查询中重复的匹配行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239613/

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