鍍金池/ 問答/數(shù)據(jù)庫/ mysql 引號(hào)變成字段問題

mysql 引號(hào)變成字段問題

突然出現(xiàn)一個(gè)引號(hào)變成字段問題

SQLSTATE[42S22]: Column not found: 1054 Unknown column '%Y-%m-%d' in 'field list' (SQL: select count(id) count,DATE_FORMAT(created_at,"%Y-%m-%d") time from `user` where `created_at` >= 2017-06-24 and `created_at` <= 2018-07-04 group by `time`)

把其中的

"%Y-%m-%d"

變成單引號(hào)

'%Y-%m-%d'

mysql 版本5.7.18 不知道這個(gè)是什么原因引起的?要如何解決呢?

回答
編輯回答
忘了我

ANSI Equivalent to REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, and (as of MySQL 5.7.5) ONLY_FULL_GROUP_BY.

ANSI_QUOTES

Treat " as an identifier quote character (like the ` quote character) and not as a string quote character. You can still use ` to quote identifiers with this mode enabled. With ANSI_QUOTES enabled, you cannot use double quotation marks to quote literal strings because they are interpreted as identifiers.

created_at >= 2017-06-24,這么用沒問題?等同數(shù)學(xué)公式created_at >= 1987

2017年9月29日 09:01
編輯回答
孤星

沒有發(fā)現(xiàn)你說的問題,日期是字符串引號(hào)括起來

SELECT
    count(id) total ,
    DATE_FORMAT(created_at , "%Y-%m-%d") time
FROM
    `user`
WHERE
    `created_at` >= "2017-06-24" # 注意這里的2017-06-24,引號(hào)括起來
AND `created_at` <= "2018-07-04"
GROUP BY
    `time`

注意這里的2017-06-24,引號(hào)括起來

注意這里的2018-07-04,引號(hào)括起來

另外就是,你用 DATE_FORMAT其實(shí)很消耗性能,還不如把你要查詢的日期先轉(zhuǎn)換成int類型再取查數(shù)據(jù)。

2017年8月23日 22:55
編輯回答
孤巷
sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ANSI"

這個(gè)后面多了一個(gè)ANSI就會(huì)出現(xiàn)我說的那個(gè)問題,現(xiàn)在去掉就不會(huì)了

2017年6月8日 03:24