SQL Question
Hi everyone,
I'm in the process of rebuilding an employment system for my work, and I've come across an SQL question, and I figured this would probably be the best place to ask.
I've got two decently complex queries already (both with one inner and two left joins) that I'm trying to combine into one query. The only thing that's different between the two queries is part of the WHERE clause:
WHERE (e.begin_date > CURDATE()) versus WHERE (e.begin_date <= CURDATE()) Other than that, the queries are identical.
How can I combine those? I'm thinking of taking the e.begin_date out of the WHERE clause and moving up to the SELECT, but with something along the lines of:
SELECT type (IF e.begin_date > CURDATE() THEN type = 'upcoming' ELSE type = 'current') ....
Is that possible? (I'm trying to combine two large table generating methods into the same method for simplicity)
Thanks,
Dave

You have two non-overlapping
You have two non-overlapping data sets so you could use a where condition that does an 'or' like
WHERE (e.begin_date > CURDATE() OR e.begin_date <= CURDATE())Note though this means you are getting the complete set so there is no need for the WHERE clause at all. Depending on your goal you make be making query simplifier at the expense of processing time, particularly if you are only generally dealing with one side of the set at a time.
Yes, I realize that. That's
Yes, I realize that. That's why I pointed out that I want to remove that WHERE clause and try and create a flag indicating which set it would be long to (the <= set or the > set).
Without trying I think
Without trying I think
type (IF e.begin_date > CURDATE() THEN type = 'upcoming' ELSE type = 'current')should be
IF( e.begin_date > CURDATE(), 'upcoming', 'current') as event_typeNote I used event_type instead of type to avoid possible conflict with the type field in the node table (may not be an issue for you)
Is this in a stored
Is this in a stored procedure? Do you want to pass a flag that says which WHERE clause to apply? What database software are you using (e.g. myql, postgresql, sql server)?