为什么我的间隔日不会在mysql语句中工作。
我想要那些超过2天的人。这就是我尝试的方式。
SELECT t.*, ct.*,cd.*, max(t.transactiontime) as maxtime
FROM exp_channel_titles as ct
LEFT JOIN transactions as t on (ct.entry_id = t.restaurant_id)
LEFT JOIN exp_channel_data as cd on (ct.entry_id = cd.entry_id)
WHERE t.cardid > 0
and maxtime < ( CURDATE() - INTERVAL 2 DAY )
and t.restaurant_id > 0
and ct.status= 'open'
Group by ct.entry_id
order by maxtime desc
这是我当前的输出
TransactionID Title TransactionTime
23132 Locaton 2013-05-17 10:02:04
23131 Novuel 2013-05-16 16:26:45
23128 Mama Rosa 2013-05-15 14:42:44
23126 Rohan 2013-05-14 12:46:56
这是我想要的输出
TransactionID Title Date
23128 Mama Rosa 2013-05-15 14:42:44
23126 Rohan 2013-05-14 12:46:56
但是我收到了这个错误
Error Number: 1054
Unknown column 'maxtime' in 'where clause'
SELECT t.*, ct.*,cd.*, max(t.transactiontime) as maxtime FROM exp_channel_titles as ct LEFT JOIN transactions as t on (ct.entry_id = t.restaurant_id) LEFT JOIN exp_channel_data as cd on (ct.entry_id = cd.entry_id) WHERE t.cardid > 0 and maxtime < ( CURDATE() - INTERVAL 2 DAY ) and t.restaurant_id > 0 and ct.status= 'open' Group by ct.entry_id order by maxtime desc
Filename: libraries/Functions.php(656) : eval()'d code
Line Number: 46
答案 0 :(得分:2)
尝试:
maxtime < DATE_FORMAT( NOW() - INTERVAL 2 DAY ,'%Y-%m-%d 23:59:59')
CURDATE()
返回DATE
类型,您需要DATETIME
类型,因此您应该使用NOW()
答案 1 :(得分:1)
你使用CURDATE,但据我所知你的日期列是datetime数据类型所以..
NOW()= 2008-11-11 12:45:34
CURDATE()= 008-11-11
CURTIME()= 12:45:34
这是mysql函数返回的内容。在这里使用正确的功能
maxtime < ( CURDATE() - INTERVAL 2 DAY )
将CURDATE()更改为NOW(),但这不是您的解决方案。你需要找出答案。
答案 2 :(得分:0)
你不能在where子句中使用别名,使用max(t.transactiontime)&lt; (现在() - INTERVAL 2 DAY)