Query Question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Query Question

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Query Question

Post 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.....
Last edited by JakeJ on Wed Jan 20, 2010 3:13 pm, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Query Question

Post 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)
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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Query Question

Post 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.
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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Query Question

Post 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.
Post Reply