Jonathan Anspaugh
Jonathan Anspaugh

Reputation: 125

Query Multiple Tables For Specific Date Range Simultaneously

I have three identical tables:

In each table, I have email and submited_date in YYYY-MM-DD format. My end goal is to query all three tables at the same time but when I do a UNION between the 3 tables for the email and submited_date columns it ignores my WHERE condition and displays all results.

My current query is as follows:

SELECT email, submited_date FROM gradbusdb.user 
UNION
SELECT email, submited_date FROM gradedudb.user
UNION
SELECT email, submited_date FROM gradtheologydb.user
WHERE submited_date BETWEEN '2016-02-01' AND '2016-02-29'
ORDER BY submited_date ASC

I am not sure what I have done wrong here but I need my query to display ONLY the results that are within the date range.

Upvotes: 0

Views: 76

Answers (1)

alexander.polomodov
alexander.polomodov

Reputation: 5534

You should use WHERE for each query into union

SELECT email, submited_date FROM gradbusdb.user 
WHERE submited_date BETWEEN '2016-02-01' AND '2016-02-29'
UNION
SELECT email, submited_date FROM gradedudb.user
WHERE submited_date BETWEEN '2016-02-01' AND '2016-02-29'
UNION
SELECT email, submited_date FROM gradtheologydb.user
WHERE submited_date BETWEEN '2016-02-01' AND '2016-02-29'

ORDER BY submited_date ASC

Upvotes: 1

Related Questions