简体   繁体   中英

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 results:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM