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.
Query Question
Moderator: General Moderators
Re: Query Question
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.....
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.....
Last edited by JakeJ on Wed Jan 20, 2010 3:13 pm, edited 1 time in total.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Query Question
Code: Select all
(SELECT * FROM tablename WHERE type_id != 1 ORDER BY nper)
UNION
(SELECT * FROM tablename WHERE type_id = 1 ORDER BY nper)mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Query Question
Yep, probably better.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!
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Query Question
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.