---增:insert into 表名(列1,列2,列3。。。) values (值1,值2,值3。。。);
---查:select 列1,列2,列3。。。 from 表名;
----刪:delect from 表名 where 條件(userid='xxx');
--改:updata 表名 set 列1=‘xxx’,列2=‘sss’,。。。 where 條件(userId like ‘h%’(模糊處理,h開頭的意思));
---------group by 子句
對前一個數(shù)據(jù)的篩選的結(jié)果進(jìn)行重組,使用某個字段或多個字段進(jìn)行分類,然后按照這些字段組合數(shù)據(jù)。注意:好像是按照字段從新得到了新表
select name from 表名 where 條件 group by name
------聚合函數(shù)
總數(shù) 最大值 最小值 平均值 求和
select name count(*),max(age),min(age),avg(age),sum(age) from 表名 group by name;
聚合函數(shù)忽略空值數(shù)據(jù)
--------開窗函數(shù)
select * avg(score) over() from 表名;(相當(dāng)于在表后面加上了avg(score))
--------having 子句
where只能直接對數(shù)據(jù)源進(jìn)行篩選
having可以對聚合后的數(shù)據(jù)進(jìn)行篩選
-----別名
select 字段 as 別名
select 字段 別名(不推薦用)
select 別名=字段
-------distinct 去除重復(fù)數(shù)據(jù)(是對整個結(jié)果集進(jìn)行數(shù)據(jù)重復(fù)處理,不是針對某一列)
select distinct * from 表名(只有完全重復(fù)才會去除,包括id);
---------top(只顯示前n行數(shù)據(jù))
select top 10 * from 表名;(只顯示前10行數(shù)據(jù))
select top 10 percent * from 表名;(只顯示前10%的數(shù)據(jù))
-------order by 字段或者表達(dá)式 (對結(jié)果進(jìn)行排序)
select * from 表名 order by 字段;默認(rèn)升序排列
select * from 表名 order by 字段 desc;降序排列
------sql語句執(zhí)行優(yōu)先級
from->where->group by-> having->select->order by->top(先排完序再取前n個數(shù)據(jù))
------時間函數(shù)
加的值
dateadd :select dateadd(DAY,1,'2001-1-1');結(jié)果2001-1-2
開始時間 結(jié)束時間
datediff:select datediff(YEAR,‘2001-1-1’,‘2003-1-1’);結(jié)果2
-----轉(zhuǎn)換函數(shù)
---cast(數(shù)據(jù) as 類型)
select ‘123’+cast(456 as varchar);
-----convert(類型,數(shù)據(jù))
select ‘123’+convert(varchar,456);
新聞熱點
疑難解答
圖片精選