分配空值以复制查询中的匹配行

时间:2017-02-15 02:21:03

标签: mysql

我是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的匹配行之外还有什么方法吗?

1 个答案:

答案 0 :(得分:1)

您要找的是CASE声明。

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

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

您的问题的答案来自我的查询的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