Avatar of Robert Saylor
Robert Saylor
Flag for United States of America

asked on 

merge two queries with MySQL

Hi, I want to merge 2 queries so I don't have to run query 1 then a nested query 2 with php. How would you merge these two as 1 query?

Query 1:
SELECT
	DISTINCT `contacts`.`contactID`,
	`contacts`.`first`,
	`contacts`.`last`,
	`contacts`.`email`

FROM
	`suspended_inventory` si, `reservations`,`contacts`
WHERE
	`si`.`reservationID` = `reservations`.`reservationID`
	AND `reservations`.`reservation_date` BETWEEN '20110101' AND '20141231'
	AND `si`.`contactID` = `contacts`.`contactID`
	AND `contacts`.`email` != ''
	AND `contacts`.`deceased` != 'Y'
	AND `contacts`.`omit_from_future_mailings` != 'Y'

ORDER BY `contacts`.`contactID`

Open in new window


I then get a contactID value. In Query 1 I would like to count the number of records returned. If 0 return 0.

Query 2
SELECT
	COUNT(`inventory`.`reservationID`) AS 'total'

FROM
	`inventory`,`reservations`

WHERE
	`inventory`.`passengerID` = '99'
	AND `inventory`.`reservationID` = `reservations`.`reservationID`
	AND `reservations`.`reservation_date` BETWEEN '20110101' AND '20141231'

Open in new window


I would like to run Query 2 inside Query 1 in the select area returning the value if possible. If not, then I will have to run this as a nested query in php.
MySQL ServerPHP

Avatar of undefined
Last Comment
Robert Saylor

8/22/2022 - Mon