sql中的扩展学习
1.
使⽤mysql进⾏delete from操作时,若⼦查询的 FROM 字句和更新/删除对象使⽤同⼀张表,会出现错误。
DELETE FROM tab1 WHERE  id = ( SELECT MAX(id ) FROM tab1 );
ERROR 1093 (HY000): You can’t specify target table ‘tab1′ for update in FROM clause
针对“同⼀张表”这个限制,撇开效率不谈,多数情况下都可以通过多加⼀层select 别名表来变通解决
2.删除重复的数据并且保留⼀条
delete from cst_customer where cst_name in (select cst_name from (SELECT  cst_name  FROM cst_customer GROUP BY cst_name having COUNT(cst_name)>1)aa)
and cst_id not in (select cst_id from (select min(cst_id) cst_id  from cst_customer GROUP BY cst_name having
COUNT(cst_name)>1) qq);
3.SELECT * FROM `cst_customer` order by if(cst_id=3,0,1),cst_id;
mysql删除重复的数据保留一条可以将cst_id=3放在最前⾯,其他按照排序
4.SELECT * FROM `cst_customer` order by field(cst_name,'wsn','4','3','a');
会将'wsn','4','3','a'按照顺序在最后展⽰
5.select REPLACE(cst_name,'-','.') from  cst_customer
查询结果将cst_name 字段中的'-'替换为'.'
6. alter table t_resource add attachment_url VARCHAR(1000) Not null comment "附件url";
添加字段  comment  注释      字符集、排序规则有默认的
7.insert  into  aa(  )时如果    aa⾥少写个字段  字段如果可为null没问题,不可为null报错

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。