MySQL database update sub-query, mysql database update
For example:
?
1234 |
UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test.tb_vobile) |
Error:
?
1234567 |
[SQL]UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test.tb_vobile) |
You can:
?
1234567891011 |
UPDATE test.tb_vobile a join (select max(id) id from test.tb_vobile) b on a.id = b.id set a.name = '123 ' ; Or UPDATE test.tb_vobile a ,(select max(id) id from test.tb_vobile) b set a.name = '321 ' WHERE a.id = b.id ; |
Note:
1. During update, the updated table cannot be used in set and where for subqueries;
2. During update, you can update multiple tables (not SQL Server );
For example, update ta a, tb B set a. Bid = B. id, B. Aid = a. id;
3. Any query can be performed after update, which is equivalent to from;
1234 |
UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test.tb_vobile) |
Error:
?
1234567 |
[SQL]UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test.tb_vobile) [Err] 1093 - You can 't specify target table ' a' for update in FROM clause |
You can:
?
1234567891011 |
UPDATE test.tb_vobile a join (select max(id) id from test.tb_vobile) b on a.id = b.id set a.name = '123 ' ; Or UPDATE test.tb_vobile a ,(select max(id) id from test.tb_vobile) b set a.name = '321 ' WHERE a.id = b.id ; |
Note:
1. During update, the updated table cannot be used in set and where for subqueries;
2. During update, you can update multiple tables (not SQL Server );
For example, update ta a, tb B set a. Bid = B. id, B. Aid = a. id;
3. Any query can be performed after update, which is equivalent to from;