Remember to solve the error once: MySQL 1054-Unknown column in where clause

1. Background introduction

This is my table:

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| na        | text         | YES  |     | NULL    |       |
| birthDate | datetime     | YES  |     | NULL    |       |
| class     | varchar(255) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

This is the code of the task I want to complete (purpose: update Zhang San’s class to Li Si’s):

UPDATE test.student class 
SET class = ( SELECT class FROM student WHERE student.na = '李四') 
WHERE
	student.na = '张三'

2. Encounter problems, search, solve problems...

1. The first problem encountered-1054:

1054-Unknown column'student.na' in'where clause'
so I deleted the code to troubleshoot possible errors

UPDATE test.student class 
SET class = 1
WHERE
	student.na = '张三'

I found that the above error still exists, delete the where clause and execute it. The error message says ['student.na' line, unrecognized], I am very puzzled, but when I delete the previous table name "student" and execute it success!

UPDATE test.student class 
SET class = 1
WHERE
	na = '张三'

So I guessed that the update command limits a certain table, and the table name cannot be superfluously added before the column name.

Then I changed the complete code to:

UPDATE test.student class 
SET class = ( SELECT class FROM student WHERE na = '李四') 
WHERE
	na = '张三'

2. See also error -1093:

1093-You can't specify target table'class' for update in FROM clause
It means that you cannot select certain values ​​in the same table first, and then update the table (in the same statement),
that is The value of the field is judged and then the value of a field is updated.

I checked it on the Internet and found that MySQL does not support this kind of operation. It is all solved by "winding"
(from: https://zhidao.baidu.com/question/68619324.html).
Solution 1: One
more level of nesting Subquery, then delete,
plan 2:
1. Create a temporary return table, automatically store the conditions to be deleted in the temporary table:
2. Delete the main table data according to the temporary table:
3. Finally delete the temporary table:

I don't want to build another table, so I used option one:

UPDATE test.student class 
SET class = (SELECT class from( SELECT class FROM student WHERE na = '李四') )
WHERE
	na = '张三'

I nested a layer outside [(SELECT class from(____))]

3. But there is another problem-1248:

(I said, this frustration comes too often, the design is so unintuitive and error-prone, no wonder programmers lose their hair)
1248-Every derived table must have its own alias
continue to search... Find the answer: https:// blog.csdn.net/cao478208248/article/details/28122113
"This sentence means that each derived table must have its own alias... The result of the subquery is as A derived table is used for the upper-level query, so the result of the subquery must have an alias "

So I imitated and added [as t]

UPDATE test.student class 
SET class = (SELECT class from( SELECT class FROM student WHERE na = '李四') as t )
WHERE
	na = '张三'

Finally successfully executed!

Guess you like

Origin blog.csdn.net/sinat_27382047/article/details/106312751