Jump to content

Combining mySQL query


petenaylor

Recommended Posts

Hi all

 

I have a issue where I need to create a mySQL query to pull four different types of result from a table and show the results.

 

mysql_query("SELECT * FROM `trade-adverts` WHERE type = 'premium' AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");

 

This works great, but I also need it to do this:

mysql_query("SELECT * FROM `trade-adverts` WHERE type = 'standard' AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");

 

and this:

mysql_query("SELECT * FROM `adverts` WHERE type = 'premium' AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");

 

and this:

mysql_query("SELECT * FROM `adverts` WHERE type = 'standard' AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");

 

Is it possible to combine this into one query? I need to paginate the results.

 

Many thanks

 

Pete

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/238319-combining-mysql-query/
Share on other sites

use for both tables:

SELECT * FROM `trade-adverts` WHERE type IN ('standard', 'premium') AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC

 

SELECT * FROM `adverts` WHERE type IN('standard', 'premium') AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC

 

now if you want to combine both tables in just one query you have to use a JOIN between them using the PK - FK in case both tables are related or use a UNION if they are not.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.