小女子求一条mysql小语句

来源:百度知道 编辑:UC知道 时间:2024/05/27 17:15:45
表 sa 表student
两字段no 和name 两字段 no和 name
当 表student中的no和表sa中的no相同时 把student中no对应的name付给表sa中no对应的name
我用update `sa` SET `name`=(select `student`.`name` from `student`,`sa` where `sa`.`no`=`student`.`no`)

MySQL 返回:

#1093 - You can't specify target table 'sa' for update in FROM clause帮助解决了再给30分

我已经测试过了,在phpmyadmin 里,
正确的答案是:
update sa,student set sa.name=student.name where sa.no=student.no;

UPDATE SA SET NAME=STUDENT.NAME
WHERE SA.NAME=STUDENT.NAME
试试看,不知行否

update sa a set a.name=(select b.name from student b where a.no=b.no);

如果不行就是你数据有问题了

我告诉你不能解决
还可以告诉你
mysql不支持这种update
一直不支持
写程序里吧
OK

insert into student
select no,name from sa
where no in(select no from student)

mysql中不能这么用。 (等待mysql升级吧)
错误提示就是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)

替换方案:

create table tmp as select `student`.`no`,`student`.`name` from `student`,`sa` where `sa`.`no`=`student`.`no`;
update sa a,tmp b set a.name=b.name where a.no=b.no;
drop table tmp;

先创建一个临时表,把符合条件的数据取出来,
然后在UPDATE
在把临时表删除!