简体   繁体   English

SQLite的Where子句中有多个条件时如何使用“或”语句?

[英]How to use the "Or" statement when having multiple conditions in the Where clause in SQLite?

I am trying to retrieve data that has "Positive" result and the code set that contains any of these codes: [U071, R12, Z515, R509] as my desired goal for Table 1. I have tried using "or" but it kept giving me a mixture of negative and positive results instead of positive results only.我正在尝试检索具有“正”结果的数据和包含以下任何代码的代码集:[U071,R12,Z515,R509] 作为表 1 的预期目标。我尝试使用“或”,但它保留给我一个消极和积极的结果,而不仅仅是积极的结果。 I hope my question is clear as I am fairly new here.我希望我的问题很清楚,因为我在这里还很新。

Desired goal for Table 1:表 1 的预期目标:

ID ID START_DATE开始日期 TEST RESULT测试结果 Code代码 END_DATE结束日期
1 1 03-31-2021 03-31-2021 positive积极的 U071 U071 03-31-2021 03-31-2021
2 2 06-06-2020 06-06-2020 positive积极的 R12 R12 06-09-2020 06-09-2020
2 2 06-06-2020 06-06-2020 positive积极的 Z515 Z515 06-10-2020 06-10-2020
3 3 06-30-2021 06-30-2021 positive积极的 R509 R509 07-02-2021 07-02-2021
4 4 08-08-2020 08-08-2020 positive积极的 U071 U071 08-10-2020 08-10-2020
4 4 08-09-2020 08-09-2020 positive积极的 Z515 Z515 08-10-2020 08-10-2020

Note: I would also like ID 2 to be grouped as 1.注意:我还希望将 ID 2 归为 1。

The problem:问题:

ID ID START_DATE开始日期 TEST RESULT测试结果 Code代码 END_DATE结束日期
1 1 03-31-2021 03-31-2021 positive积极的 U071 U071 03-31-2021 03-31-2021
2 2 06-05-2020 06-05-2020 negative消极的 R12 R12 06-09-2020 06-09-2020
2 2 06-05-2020 06-05-2020 negative消极的 Z515 Z515 06-09-2020 06-09-2020
3 3 06-03-2021 06-03-2021 positive积极的 R509 R509 07-02-2021 07-02-2021
4 4 08-10-2020 08-10-2020 negative消极的 U071 U071 08-10-2020 08-10-2020
4 4 08-06-2020 08-06-2020 positive积极的 Z515 Z515 08-10-2020 08-10-2020

My code:我的代码:

CREATE TABLE table1 AS
select   ID
        ,TEST_RESULT  
        ,START_DATE
        ,END_DATE
        ,Code
from table0
where TEST_RESULT = 'POSITIVE' AND Code = 'U071' OR Code = 'Z20828' OR DIAGNOSIS_CD = 'Z20822' OR DIAGNOSIS_CD = 'R112' OR DIAGNOSIS_CD = 'R509'
group by ID, START_DATE
ORDER BY ID;

I think the real issue is you did not use ( after the and. But you can use IN to make it clearer -- like this:我认为真正的问题是你没有使用 ( 在 and 之后。但你可以使用 IN 让它更清楚 - 像这样:

where TEST_RESULT = 'POSITIVE' AND (Code IN ('U071','Z20828') OR DIAGNOSIS_CD IN ('Z20822','R112','R509'))

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

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