Solved

merge two queries with MySQL

Posted on 2014-12-03
2
268 Views
Last Modified: 2014-12-03
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.
0
Comment
Question by:Robert Saylor
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
something like this should be a starter:

SELECT	`contacts`.`contactID`,
	`contacts`.`first`,
	`contacts`.`last`,
	`contacts`.`email`
  ,  (SELECT COUNT(`inventory`.`reservationID`)
   FROM	`inventory`,`reservations`
  WHERE `inventory`.`passengerID` = '99'
	AND `inventory`.`reservationID` = `reservations`.`reservationID`
	AND `reservations`.`reservation_date` BETWEEN '20110101' AND '20141231'
  )   AS 'total'
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

tip: use aliases in your queries:
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html
0
 
LVL 6

Author Closing Comment

by:Robert Saylor
Comment Utility
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now