コードをそのまま挿入するとNULLになります
INSERT INTO dates VALUES (date='12-11-20 11:30:11');
クエリを起動すると
SELECT * FROM dates;
私は得る
|dates |
| NULL |
INSERT INTO dates (date) VALUES('12-11-20 11:30:11')
あなたの構文は間違っています。
正しい構文は次のとおりです。
INSERT INTO dates (dates) VALUES ('12-11-20 11:30:11');
あなたのINSERT
クエリは間違っています。次の構文のいずれかを使用します。
INSERT INTO dates (date) VALUES ('12-11-20 11:30:11')
INSERT INTO dates SET date = '12-11-20 11:30:11'
前者はすべてのデータベースで機能するため優先されますが、後者は標準 SQL ではない可能性が高いです。
2012-11-20
の代わりに使用することも検討する必要があり12-11-20
ます。人々が 4 桁の年を使用していないために、12 年前に何が起こったかは誰もが知っています。
Well, there are several interesting issues to note here.
First, it's safer in my opinion to explicitly state the fields which will be updated: otherwise the query becomes unusable after the table's structure changes. And it's not recommended to give the same name to the table's field and the table itself: it may confuse you later.
Second, your DATETIME literal is actually ok for MySQL - it accepts two numbers for a year, parsing it by a specific rule:
Year values in the range 70-99 are converted to 1970-1999.
Year values in the range 00-69 are converted to 2000-2069.
So you may still use '12-11-20' syntax... if, again, it won't become confusing to whoever will maintain your script later (even you, perhaps.. )).
Finally, INSERT expression can take any valid expression within the VALUES clause. In your case it's date = '...'
expression, which is evaluated to NULL; that's why NULL gets inserted in your table. If you, however, want to insert only the DATETIME literal (and you probably do), drop the assignment sign (which is actually taken as 'equality operator') and use simply...
INSERT INTO dates (datetime_field) VALUES ('2012-11-20 12:12:12');
... or this, if you want to insert several rows with a single query:
INSERT INTO dates (datetime_field) VALUES ('2012-11-20 12:12:12'), ('2012-11-20 13:13:13');
Hope this helps. ) But even if you don't have any questions, I'd still recommend studying Insert syntax and Date and time literals topics anyway - it WILL be helpful.