Re: Basic Dual Query Combine into one question
| From: | Piers | Date: | Fri, 23 Jan 2015 02:26:30 +0000 |
| Subject: | Re: Basic Dual Query Combine into one question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324277@lists.php.net to get a copy of this message | ||
On 23/01/15 11:50, dealTek wrote:
Hi all, (newbie question) - let's say, (for some other reasons I'll skip here) I needed to do two separate queries to the same mysql contacts table (rather than one more elegant combined one) 1 - find everybody from California 2 - find everybody from Texas Then – I would like to combine both of these queries into some kind of an array that I could loop through and display after sorting by last name Q: I am curious the best way to combine the 2 separate queries into 1 afterwards?Hi dealTek, If you are using MySQL you could look at the UNION clause. It allows you to do two very different queries and combine them into one result set / array. 1st hit I could find. http://www.w3schools.com/sql/sql_union.asp eg: SELECT City, State FROM contacts WHERE State='California' AND cat_lover = 'Yes' UNION ALL SELECT City, State FROM contacts WHERE State='Texas' AND has_hats = 'No' ORDER BY City; Would that achieve the same result in one query? Cheers P