Page 1 of 1
Query Question
Posted: Wed Jan 20, 2010 2:49 pm
by JakeJ
In MySQL can I append the results of one query to another? I understand how joins work but I need to append the results of one query to those of another all from the same table.
For example:
SELECT * FROM table where type_id != 1 ORDER BY nper;
append the following to the previous query.
SELECT * FROM table where type_id = 1 ORDER BY nper;
I have a very specific reason for needing type_id 1 at the end of the results regardless of the order of nper.
Thanks in advance.
Re: Query Question
Posted: Wed Jan 20, 2010 3:00 pm
by JakeJ
Nevermind, I think multiple ORDER BY clauses will do it for me.
SELECT * FROM table ORDER BY type_id DESC, nper seems to produce the results I need.
I wish I would have thought about that BEFORE I posted!
**EDITING***
OK, I didn't get the results I needed. Back to square one.
First Query: SELECT * from table WHERE type_id != 1 ORDER BY nper;
I want to append the following to the first query:
Second Query: SELECT * from table WHERE type_id = 1 ORDER by nper;
Multiple order by clauses sort nper in a way I don't want. I want everything sorted by nper EXCEPT type_id = 1.
HELLLPPP.....
Re: Query Question
Posted: Wed Jan 20, 2010 3:01 pm
by AbraCadaver
Code: Select all
(SELECT * FROM tablename WHERE type_id != 1 ORDER BY nper)
UNION
(SELECT * FROM tablename WHERE type_id = 1 ORDER BY nper)
Re: Query Question
Posted: Wed Jan 20, 2010 3:01 pm
by AbraCadaver
JakeJ wrote:Nevermind, I think multiple ORDER BY clauses will do it for me.
SELECT * FROM table ORDER BY type_id DESC, nper seems to produce the results I need.
I wish I would have thought about that BEFORE I posted!
Yep, probably better.
Re: Query Question
Posted: Wed Jan 20, 2010 3:16 pm
by JakeJ
AbraCadaver wrote:Code: Select all
(SELECT * FROM tablename WHERE type_id != 1 ORDER BY nper)
UNION
(SELECT * FROM tablename WHERE type_id = 1 ORDER BY nper)
That was exactly what I needed!
Thanks!
*PS. I was editing my last entry when you responded.