1

尝试运行此查询:

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"}
4

2 回答 2

1

count保留字,不能作为别名,使用不同的名字

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

或带反引号

(offers_consumers_history.date,'%d-%m-%Y') AS `date`,
COUNT(*) AS `COUNT`
于 2013-06-24T10:18:13.737 回答
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`
于 2013-06-24T10:18:58.770 回答