mrd
mrd

Reputation: 2183

Merge two SQL queries

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

Answers (10)

Ralph B. Roman
Ralph B. Roman

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

prateek
prateek

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

Waqar Ahmed Khan
Waqar Ahmed Khan

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

Barry McGillin
Barry McGillin

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

Kevin Holditch
Kevin Holditch

Reputation: 5303

SELECT * 
FROM   TABLE1 
WHERE  ( CHAPTER = 88 
         AND SENTENCE >= 23 ) 
        OR ( CHAPTER = 89 
             AND SENTENCE >= 1 
             AND SENTENCE <= 23 ) 

Upvotes: 3

timtour97
timtour97

Reputation: 222

Try this:

SELECT * 
FROM   TABLE1 
WHERE  ( CHAPTER = 88 
         AND SENTENCE >= 23 ) 
        OR ( CHAPTER = 89 
             AND SENTENCE BETWEEN 1 AND 23 ); 

Upvotes: 2

evilone
evilone

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

OTTA
OTTA

Reputation: 1081

SELECT * 
  FROM table1
 WHERE (chapter = 88 AND sentence >= 23)
    OR (chapter = 89 AND sentence >= 1 AND sentence <= 23)

Upvotes: 1

Alex Szabo
Alex Szabo

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

Nick.Mc
Nick.Mc

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

Related Questions