Storing a mysql datetime in a user variable
I have quite a complex MySQL query with a problem but I have narrowed the problem down to the SQL below. The problem is that the MySQL date functions (WEEK, YEAR etc.) don't accept the datetime stored in a user variable.
SELECT
@test := datetime
,datetime
FROM `agenda`
WHERE YEAR(@test) = 2011
This doesn't give me any results, however, the following SQL gives me resul开发者_StackOverflowts:
SELECT
@test := datetime
,datetime
FROM `agenda`
WHERE YEAR(datetime) = 2011
(datetime is a fieldname in the agenda table.)
What is the problem here?
In the first query you're attempting to set @test equal to the datetime fields value based on a WHERE clause that is itself referring to the value of @test.
Unless @test has a value up front then you can't expect this to produce any meaningful results.
精彩评论