0

I have two queries I am trying to combine into one. A simplified version of what I am doing:

SELECT * FROM table WHERE `id` = '1' ORDER BY `name`
SELECT * FROM table WHERE `id` = '2' ORDER BY `age`

I have looked into unions but the examples I find online are less than helpful.

8
  • 2
    I think you are looking for union or union all. Also which DBMS are you using? Commented Nov 11, 2016 at 10:08
  • where id in (1,2). Commented Nov 11, 2016 at 10:08
  • 1
    Seems odd. Can you add your IN-query to the question? Commented Nov 11, 2016 at 10:18
  • 1
    Which dbms are you using? Commented Nov 11, 2016 at 10:20
  • 3
    Which Database you are using ? as 'IN' should totally work Commented Nov 11, 2016 at 10:21

4 Answers 4

1

You might be looking for something like this:

SELECT * FROM YourTable 
WHERE id in('1','2') 
ORDER BY id,CASE WHEN id=1 THEN age END,CASE WHEN id=2 THEN name END

As the id is part of the resultset, there is no need for UNION or other complicated tricks.

I'm quite sure, that age and name are not the same type. One single CASE WHEN might get into troubles here.

In my suggestion the ORDER BY uses three columns. This will translate to

ORDER BY 1,age,NULL

or to

ORDER BY 2,NULL,name
Sign up to request clarification or add additional context in comments.

4 Comments

While this does work and I can kinda see why, I am curious how you would modify the order. So for example, if I wanted age to be ASC and name to be DESC.
That was what I assumed, but I got back an 'invalid syntax' error.
To be clear though, you mean: ORDER BY id,CASE WHEN id=1 THEN age ASC END,CASE WHEN id=2 THEN name DESC END
@Robbie No, it is ORDER BY id,CASE WHEN id=1 THEN age END ASC,CASE WHEN id=2 THEN name END DESC. Think of the CASE WHEN ... END as one single value...
0

The below code will simply put together the results of each. There isn't much more to it.

SELECT * FROM table WHERE `id` = '1' ORDER BY `name`
union
SELECT * FROM table WHERE `id` = '2' ORDER BY `age`

5 Comments

Those ORDER BY's are invalid, and make no sense.
This wouldn't run?
Not in ANSI SQL. Perhaps some less strict dbms product will execute it?
It didn't run for me either using mysql.
Besides the fact, that this is invalid syntax (the ORDER BY must be applied to the outer-most select) there is absolutely no use for union at all... This is procedural thinking. The server will not work things down in the order you'd expect this. You might use a table with an IDENTITY column and insert both statements one after the other. In this case your table would combine both resultsets in the way you seem to expect it here...
0

Apply the Union operator between both queries. Please take care about selecting same column name and sequence into both the queries like below:

SELECT id,columnname1,columnname2 FROM table WHERE id = '1'
union
SELECT id,columnname1,columnname2 FROM table WHERE id = '2'

1 Comment

This exactly the same as select ... from table where id in(1,2) just with a lot of overhead...
0

select * from (select id from table1 where id=1 order by id) UNION select * From (select id from table2 where id=2 order by name ); this is working fine with oracle.

1 Comment

1) There is no need for UNION at all (see existing answers) and 2) Only the outer-most ORDER BY is relevant. This call might return in differing order from call to call...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.