向FROM_UNIXTIME添加日期格式会中断查询

时间:2013-06-24 10:16:08

标签: mysql

尝试运行此查询:

SELECT FROM_UNIXTIME(offers_consumers_history.date,
                     `'%d-%m-%Y')` AS date,
                     COUNT(*) AS COUNT
FROM (`offers_history`)
JOIN `offers_consumers_history` ON `offers_consumers_history`.`offer_history_id`=`offers_history`.`id`
WHERE `offers_history`.`merchant_id` = 1
  AND `offers_history`.`offer_type_id` = 1
GROUP BY DATE(FROM_UNIXTIME(offers_consumers_history.date))
ORDER BY `offers_consumers_history`.`date`

如果我在第一个FROM_UNIXTIME(%d-%m-%Y部分)上没有日期格式的情况下运行它,一切运行正常,但我显然没有显示正确的日期格式,返回的错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (`offers_history`) JOIN `offers_consumers_history` ON `offers_consumers_his' at line 2

没有日期格式我得到的结果如下:

{"date":"2010-08-18 18:40:00","count":"2"}

我想要的是:

{"date":"18-08-2010","count":"2"}

2 个答案:

答案 0 :(得分:1)

countreserved word,您不能将其用作别名,使用不同的名称

'%d-%m-%Y') AS dt,
COUNT(*) AS cnt

OR与反引号

(offers_consumers_history.date,'%d-%m-%Y') AS `date`,
COUNT(*) AS `COUNT`

答案 1 :(得分:0)

从表名中删除()并更改count的别名:

SELECT FROM_UNIXTIME(offers_consumers_history.date,
                     '%d-%m-%Y') AS `date`,
                     COUNT(*) AS cnt
FROM `offers_history`
JOIN `offers_consumers_history` ON `offers_consumers_history`.`offer_history_id`=`offers_history`.`id`
WHERE `offers_history`.`merchant_id` = 1
  AND `offers_history`.`offer_type_id` = 1
GROUP BY DATE(FROM_UNIXTIME(offers_consumers_history.date))
ORDER BY `offers_consumers_history`.`date`