Reputation: 2183
How to combine two SQL queries in one?
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23
Upvotes: 1
Views: 399
Reputation: 361
You don't need any Union or Merge statement those operations are redundant. Just change the conditions of your first select.
SELECT *FROM table1 WHERE (chapter=88 OR chapter=89) AND
sentence>= (CASE WHEN chapter = 88
THEN 23
WHEN chapter = 89
THEN 1
END ) AND
sentence <= (CASE WHEN chapter = 89
THEN 23
ELSE sentence
END)
Upvotes: 0
Reputation: 25
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
UNION
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23
Upvotes: -1
Reputation: 21
Select *
From table1
where ((chapter=88 AND sentence>=23)
OR (chapter=89 AND sentence>=1 AND sentence<=23));
Also, you can use the BETWEEN
keyword in the second clause.
Upvotes: 0
Reputation: 495
You've two ways to do it with a table like this and some sample data
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);
You can use a union to munge the two sets together
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
UNION
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23;
or merge the predicates in the where clause to achieve the same thing
SELECT *FROM table1 WHERE (chapter=88 AND sentence>=23) or
(chapter=89 AND sentence>=1 AND sentence<=23);
both will give this result
| ID| CHAPTER| SENTENCE|
----------------------------------
| 3| 88| 25|
| 4| 89| 15|
Upvotes: 2
Reputation: 5303
SELECT *
FROM TABLE1
WHERE ( CHAPTER = 88
AND SENTENCE >= 23 )
OR ( CHAPTER = 89
AND SENTENCE >= 1
AND SENTENCE <= 23 )
Upvotes: 3
Reputation: 222
Try this:
SELECT *
FROM TABLE1
WHERE ( CHAPTER = 88
AND SENTENCE >= 23 )
OR ( CHAPTER = 89
AND SENTENCE BETWEEN 1 AND 23 );
Upvotes: 2
Reputation: 22740
You can join the queries like this:
SELECT * FROM table1
WHERE
(chapter = 88 AND sentence >= 23)
OR
(chapter = 89 AND sentence >= 1 AND sentence <= 23)
Upvotes: 5
Reputation: 1081
SELECT *
FROM table1
WHERE (chapter = 88 AND sentence >= 23)
OR (chapter = 89 AND sentence >= 1 AND sentence <= 23)
Upvotes: 1
Reputation: 3266
Try this:
SELECT * FROM table1
WHERE (chapter=88 and sentence>=23)
OR (chapter=89 and sentence>=1 and sentence<=23)
This should return the results from both queries.
Upvotes: 1
Reputation: 19184
This is one way
SELECT *FROM table1 WHERE chapter=88 AND sentence>=23
UNION ALL
SELECT *FROM table1 WHERE chapter=89 AND sentence>=1 AND sentence<=23
but you should get into the habit of explicitly listing columns. The columns must align or it won't work.
Here's another way
SELECT * FROM table1
WHERE (chapter=88 AND sentence>=23) OR (chapter=89 AND sentence>=1 AND sentence<=23)
Upvotes: 5