mysql 对同一个表select 和update

mysql不会让你在对一个表进行update的同时运行select

变通的方式是衍生表,生成一张临时表。这样就可以既update又select

update tb_trade_1 inner join (select  tid ,count(*) as cnt  from tb_order_1 group by tid) as der using(tid) set commission_fee= der.cnt;

mysql中去除重复的数据

按常理来说,这个sql语句应该是:
delete tablename where id not in(select min(id) from tablename group by name,kecheng,fenshu);
这种写法在sqlserver或者oracle中是支持的,但是mysql目前是不支持的,会报类似错:You can't specify target table 'tablename' for update ,这是因为在mysql中不能同时查询一个表的数据再同时进行删除.

delete   a.* from  tb_trade_0 as a,( select tid,count(*),min(id) as id  from tb_trade_0
group by tid  having count(*)>1 order by gmt_create desc) as b
 where a.tid=b.tid and a.id > b.id;

DELETE a.* FROM `sites_cates` AS a LEFT JOIN `sites` AS b ON `a`.`site_id` = `b`.`sid` WHERE `b`.`name` IS NULL 这样不就简单了,mysql与oracle的不同点是mysql不能在从句中使用主句使用的表,但是oracle是可以的,这是mysql和oracle的sql的不同点之一

你可能感兴趣的:(mysql)