简体   繁体   English

在用户变量中存储mysql datetime

[英]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. 我有一个非常复杂的MySQL查询,但有一个问题,但我已将问题缩小到下面的SQL。 The problem is that the MySQL date functions (WEEK, YEAR etc.) don't accept the datetime stored in a user variable. 问题是MySQL日期函数(WEEK,YEAR等)不接受存储在用户变量中的datetime。

SELECT
@test := datetime
,datetime
FROM `agenda`
WHERE YEAR(@test) = 2011

This doesn't give me any results, however, the following SQL gives me results: 这不会给我任何结果,但是,以下SQL给我结果:

SELECT
@test := datetime
,datetime
FROM `agenda`
WHERE YEAR(datetime) = 2011

(datetime is a fieldname in the agenda table.) (datetime是议程表中的字段名。)

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. 在第一个查询中,您尝试根据本身引用@test的WHERE子句将@test设置为等于datetime字段值。

Unless @test has a value up front then you can't expect this to produce any meaningful results. 除非@test先有一个值,否则您不能指望它会产生任何有意义的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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