I need help combining two tables in my MySQL database. First, here are my two tables:
TABLE: events TABLE: staticEvents
id - int id - int
eventName - text eventName - text
eventDate - date eventMonth - tinyint
eventLength - int eventDay - tinyint
eventLength - int
What I am looking for is an output similar to this, when I make a query for the current month/year:
I intentionally made the “date” in staticEvents the way I did for a reason, so I can’t really change it. If anybody could help a guy out with a query, I would really appreciate it.
Thanks for taking the time to read, and have a great day.
SELECT eventName
, eventDate
, eventLength
FROM events
UNION ALL
SELECT eventName
, CAST(CONCAT_WS('-','2011',eventMonth,eventDay) AS DATE)
, eventLength
FROM staticEvents
ORDER
BY eventDate
HEY … that is great, thank you! I made a small change to look for events for the current month/year. What do you think?
SELECT eventName,eventDate,eventLength
FROM events where eventDate like '2011-12%'
UNION ALL SELECT eventName,
CAST(CONCAT_WS('-','2011',eventMonth,eventDay) AS DATE), eventLength
FROM staticEvents where eventMonth = '12'
ORDER BY eventDate, eventName"
where eventDate >= '2011-12-01'
and eventDate < '2012-01-01'
mysql is a very forgiving database, but relying on implicit conversions is not a winning strategy in the long run
it’s better to code explicitly with the correct datatype in mind, and the range test is best practice for dates because it also allows for the query to be optimized (i.e. use an index)