1. 程式人生 > >幾條最近常用的sql語句

幾條最近常用的sql語句

sql語句 total HA ont let where select查詢 新的 做的

1.查詢表是否為空

select count(*) as total from 表

看返回值,如果>0則不為空 =0為空

2.刪除表數據但保留表結構

delete from 表 where 1=1

3.select查詢結果保存到一個新的表中

有2種寫法:

其一:insert into 新表(要求在數據庫存在) select 表項 from 已有的表

其二:select * into 新表(要求在數據庫不存在) from ( select 表項 from 表 )a

這2者傾向第一種,因為第二種每次都要建立新表,浪費空間,第一種在保存之前把表數據刪了就好,不過有個缺點,重復使用同一個表會出現2次插入,我自己做的就是,還沒找出來bug

4.按照年份排序,date數據的類型

select convert(varchar(4),date,120)as year from 表 group by convert(varchar(4),date,120)

5.將date類型的數據按照月份查詢

太長了就不貼了

(未完待續……)

幾條最近常用的sql語句