如何将两个SQL查询合并为一个?
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23
答案 0 :(得分:5)
这是一种方式
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
UNION ALL
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23
但你应该养成明确列出列的习惯。列必须对齐或不起作用。
这是另一种方式
SELECT * FROM table1
WHERE (chapter=88 AND sentence>=23) OR (chapter=89 AND sentence>=1 AND sentence<=23)
答案 1 :(得分:5)
您可以加入以下查询:
SELECT * FROM table1
WHERE
(chapter = 88 AND sentence >= 23)
OR
(chapter = 89 AND sentence >= 1 AND sentence <= 23)
答案 2 :(得分:2)
SELECT *
FROM TABLE1
WHERE ( CHAPTER = 88
AND SENTENCE >= 23 )
OR ( CHAPTER = 89
AND SENTENCE >= 1
AND SENTENCE <= 23 )
答案 3 :(得分:2)
试试这个:
SELECT *
FROM TABLE1
WHERE ( CHAPTER = 88
AND SENTENCE >= 23 )
OR ( CHAPTER = 89
AND SENTENCE BETWEEN 1 AND 23 );
答案 4 :(得分:2)
你有两种方法可以使用这样的表和一些示例数据
create table table1 (id number, chapter number, sentence number);
Insert into TABLE1 (ID,CHAPTER,SENTENCE) values (1,65,24);
Insert into TABLE1 (ID,CHAPTER,SENTENCE) values (2,22,22);
Insert into TABLE1 (ID,CHAPTER,SENTENCE) values (3,88,25);
Insert into TABLE1 (ID,CHAPTER,SENTENCE) values (4,89,15);
Insert into TABLE1 (ID,CHAPTER,SENTENCE) values (4,89,33);
您可以使用联合将两个集合组合在一起
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
UNION
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23;
或合并where子句中的谓词以实现相同的目的
SELECT *FROM table1 WHERE (chapter=88 AND sentence>=23) or
(chapter=89 AND sentence>=1 AND sentence<=23);
两者都会给出这个结果
| ID| CHAPTER| SENTENCE|
----------------------------------
| 3| 88| 25|
| 4| 89| 15|
答案 5 :(得分:1)
试试这个:
SELECT * FROM table1
WHERE (chapter=88 and sentence>=23)
OR (chapter=89 and sentence>=1 and sentence<=23)
这应该返回两个查询的结果。
答案 6 :(得分:1)
SELECT *
FROM table1
WHERE (chapter = 88 AND sentence >= 23)
OR (chapter = 89 AND sentence >= 1 AND sentence <= 23)
答案 7 :(得分:0)
Select *
From table1
where ((chapter=88 AND sentence>=23)
OR (chapter=89 AND sentence>=1 AND sentence<=23));
此外,您可以在第二个子句中使用BETWEEN
关键字。
答案 8 :(得分:-1)
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
UNION
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23