Hi.. I'm trying to extract the day, month, and year from a datetime to use it within a 'WHERE' statement.

Table: workshops
workshopid - int(10)
workshopgroupcoursecode - text
title - varchar(255)
description - text

Table: workshops
workshopscheduleid - int(10)
workshopid - int(10)
startdatetime - datetime
enddatetime - datetime
timedescription - text


SELECT workshops.workshopid, EXTRACT(DAY FROM workshopschedules.startdatetime) as d, workshops.title,
FROM workshopschedules,  workshops
WHERE Month(workshopschedules.startdatetime) = '$month' AND Year(workshopschedules.startdatetime) = '$year'";

****You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM workshopschedules, workshops WHERE Month(workshopsched

does mysql not allow me to extract the day and month and use it in a WHERE condition??

Recommended Answers

All 4 Replies

It's telling you exactly where to look for the error. Hint: look at the first non-whitespace character before 'FROM'.

Give up? OK. You have a trailing comma in your AS clause. (You've no idea how many times I've hit this in the past 7 years just from editting a script or a schema.) Otherwise, the syntax looks OK.

When the error says to look 'near', it usually means to look before. In time and with practice, your brain will learn to grok what your eye is seeing. :) :)

thank-you.... =D

select * from <tn> where year(date_mem)='yyyy';
select * from table where DATE(field)='yyyy-mm-dd'
commented: dude, its four years old, the op has left the building, and by the way, your answer is wrong, -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.