---
title: "You can't specify target table 'sc1' for update in FROM clause"
author: "Speeddsy"
date: "2012-06-27T00:54:00Z"
tags: []
categories: ["数据库"]
slug: "you-can-t-specify-target-table-sc1-for-update-in-from-clause"
draft: false
lastmod: "2012-11-09T10:14:50Z"
id: 1721441
url: "https://speeddsy.iteye.com/blog/1721441"
---

更新的时候子查询不能直接select更新的表

如这个是错的

```
update sc
 set sc.grade=(select avg(sc1.grade) from sc as sc1 where 
   sc1.cno=sc.cno and sc1.sno in(select s1.sno from s as s1 where s1.sex='M'))
  where sc.sno in(select s.sno from s where s.age>30 and s.sex='M');
```

否则将出现错误：ERROR 1093 (HY000): You can't specify target table 'sc1' for update in FROM clause

下面是对的

```
update sc
 set sc.grade=(select avg(sc1.grade) from (select * from sc) as sc1 where  
   sc1.cno=sc.cno and sc1.sno in(select s1.sno from s as s1 where s1.sex='M'))
  where sc.sno in(select s.sno from s where s.age>30 and s.sex='M');
```