【MySQL】SQL语句嵌套1

mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:

例如:

DELETE FROM jx_test1 WHERE user_id = 

(SELECT user_id FROM jx_test1 

WHERE createtime < '2013-10-01' ORDER BY User_id  LIMIT 1)

错误码: 1093
You can't specify target table 'jx_test1' for update in FROM clause

Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000

 

修改为:

DELETE FROM jx_test1 WHERE user_id = 

(SELECT a.user_id FROM 

  (SELECT user_id FROM jx_test1 WHERE createtime < '2013-10-01' ORDER BY User_id LIMIT 1)

a);

也就是说将select出的结果再通过中间表select一遍,这样就规避了错误

 

SELECT host.ip,host.name AS host_name,SUBSTRING(`db_instance`.`name`,1,LENGTH(`db_instance`.`name`)-14) AS instance_name,db_instance.port 

FROM `host` JOIN `db_instance` ON host.id=db_instance.hostID  WHERE db_instance.name LIKE '%ft%' ORDER BY host.name

 

 

你可能感兴趣的:(mysql)