sql中的擴充套件學習

bingguang1993發表於2018-05-06

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;

 可以將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報錯

 


 

相關文章