将 mysql 日期时间存储在用户变量中
我有一个相当复杂的 MySQL 查询,但有一个问题,但我已将问题范围缩小到下面的 SQL。问题是 MySQL 日期函数(WEEK、YEAR 等)不接受存储在用户变量中的日期时间。
SELECT
@test := datetime
,datetime
FROM `agenda`
WHERE YEAR(@test) = 2011
这没有给我任何结果,但是,以下 SQL 给了我结果:(
SELECT
@test := datetime
,datetime
FROM `agenda`
WHERE YEAR(datetime) = 2011
日期时间是议程表中的字段名。)
这里有什么问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个查询中,您尝试根据 WHERE 子句将 @test 设置为等于日期时间字段值,该子句本身引用 @test 的值。
除非 @test 预先有一个值,否则您不能指望它会产生任何有意义的结果。
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.