“Unknown column in 'field list'”, but colu

2019-01-15 00:05发布

DROP TABLE IF EXISTS `transactions`;
CREATE TABLE `transactions` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `purchase_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `transactions` (`purchase_date`) VALUES (NULL)

I've isolated my problem in this code. When I run it, I get the error:

[ERROR in query 3] Unknown column 'purchase_date' in 'field list'

Anyone an idea?

6条回答
Luminary・发光体
2楼-- · 2019-01-15 00:33

Same error in a different scenario:

This also happens when you miss @ symbol for a variable.

SET @myVar1=1;  SELECT @myVar1;  -- GOOD, correctly prints:  1
SET @myVar1=1;  SELECT  myVar1;  -- BAD, prints:  Unknown column 'myVar1' in 'field list' 
查看更多
可以哭但决不认输i
3楼-- · 2019-01-15 00:37

This might not help anyone else, but adding this "just in case" it helps someone.

In my situation it was a different solution.

I receive large datasets as Excel CSV files and use a (WIL) script to convert the .csv file into an importable .sql file. I had an error in my script whereby these two lines did not reference the same table name (I had hard-coded the first location and forgot to update it):

* "INSERT INTO `old_table_name` (`cid`, `date`, etc etc"
* "CREATE TABLE IF NOT EXISTS `":_dbName:"` (etc etc "

I just changed the first line to also get the table name from the variable, and voila!

* "INSERT INTO `":_dbName:"` (`cid`, `date`, etc etc"

So check those two lines in your import SQL file.

查看更多
放荡不羁爱自由
4楼-- · 2019-01-15 00:38

This can also happen if you paste a column name when building the table structure. Same error - but the unprintable/invisible characters are in the table structure, not the query.

查看更多
地球回转人心会变
5楼-- · 2019-01-15 00:42

There is an unprintable character 30 (RecordSeparator) inserted between purchase_date and the ' in the INSERT statement. Just remove the text ('purchase_date') and rewrite it by hand it should be fine.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-15 00:55

I just spent the better part of a day figuring this out. My problem was the same: invisible characters kiboshing the query and returning the "unknown column" error.

I solved it by wading back into Windows and removing the garbage using NotePad++.

How did the garbage get in there in the first place? I think it was because I made the mistake of copying some long complex queries into LibreOffice Writer (my functional specs document) instead of just bookmarking them in phpMyAdmin or saving them in a text editor. Pasting them from LibreOffice into the query window is where (I think) the garbage originated.

Once there, it persisted like Malaria. I couldn't even get rid of it by hand-retyping the whole query -- I had to put it into NotePad++ (Encoding menu) and show ANSI and the UTF8 combos and then remove the garbage by hand.

Once that was done, the query worked.

查看更多
聊天终结者
7楼-- · 2019-01-15 00:55

Nery niche solution when I got this error.

I had a BEFORE INSERT trigger on my table that did something with NEW.`field_mysql_doesnt_think_exists` and if I didn't pass that field to an insert statement then I would get

[ERROR in query 3] Unknown column 'field_mysql_doesnt_think_exists' in 'field list'

查看更多
登录 后发表回答