Solved

merge two queries with MySQL

Posted on 2014-12-03
2
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40478478
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 7

Author Closing Comment

by:Robert Saylor
ID: 40478495
Thanks!
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 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