“增查改删”的语句
select * from UserInfor --查找所有字段select username,UserId from UserInfor -- 查找username,UserId兩個字段select top 2* from UserInfor where (Major='計算機(jī)' or Major='土木工程') and Sex=0 order by Age desc --降序select top 2* from UserInfor where major in('計算機(jī)','土木工程') and Sex=0 order by Age --升序select * from UserInfor where Age > -- max() min() Avg()(
select Avg(Age) from UserInfor
) select sum(Age) from UserInfor -- 年齡總和select count(*) as UserCount from UserInfor where Major='計算機(jī)' -- count(*) 代表某一列有多少個, as 代表別名select count(*) as UserCount,Major from UserInfor group by major -- 按照major計算分別有多少人select count(*) as UserCount,Major from UserInfor group by Major having count(*) >2 -- 這里having是配合group by使用,這里不能用whereselect * from UserInfor where RealName like '%張%' -- 模糊查詢 “張%”以張開頭;“%張”以張結(jié)尾;“%張%”包含張select distinct realname from userinfor -- 只顯示列中不同的值,不重復(fù)的數(shù)據(jù)select top 2 * -- 分頁語句
from
(select row_number() over(order by userId) as rownumber,* from userinfor -- row_number() over找出行號, 以userid排序,別名 rownumber
) A
where rownumber > 2 -- >0第一頁;>2第二頁 ...select UserID,Age, -- case when 語句
case
when age>=20 and age<=24 then '大一'
when age>=25 and age<=28 then '大二'
else '大三'
end as usergrade from userinforselect * from UserInfor where age between 20 and 24 -- betwwen and的用法select UserId ,username, ISNULL (classname,'四班') from UserInfor -- 判斷classname字段有沒有NULL,有NULL的設(shè)定值為'四班'select year(getdate()) -- 獲取當(dāng)前時間的年 Month(getdate()) day(getdate())select * from UserInfor where year(getdate())-year(Birthday )>25 -- 找出年齡大于25歲的員工信息select DATEADD (yy,100,getdate()) -- yy代表年, 當(dāng)前時間加上100年;mm代表月;dd代表天select DATEDIFF (yy,getdate(),'2018/12/31') -- yy代表年,當(dāng)前時間距離2018/12/31多少年;mm代表月;dd代表天select UI.userid,UI.username,UI.qq,UI.realname,SS.Scoreid,ss.chinesescore --聯(lián)合查詢的三種方法,以左邊為準(zhǔn),右邊沒有的用NULL補(bǔ)
from UserInfor UI left join stuscore SS on
UI.UserId =SS.UserId select UI.userid,UI.username,UI.qq,UI.realname,SS.Scoreid,ss.chinesescore --聯(lián)合查詢的三種方法,以右邊為準(zhǔn),左邊沒有的用NULL補(bǔ)
from UserInfor UI right join stuscore SS on
UI.UserId =SS.UserId select UI.userid,UI.username,UI.qq,UI.realname,SS.Scoreid,ss.chinesescore --聯(lián)合查詢的三種方法,兩邊都有的值
from UserInfor UI inner join stuscore SS on
UI.UserId =SS.UserId select UIR.*,PP.MotherName,PP.fathername from
(select UI.userid,UI.username,UI.qq,UI.realname,SS.Scoreid,ss.chinesescore --三個表的聯(lián)合查詢,超過四個表的聯(lián)合查詢,就是需求有問題,要用到緩存
from UserInfor UI inner join stuscore SS on
UI.UserId =SS.UserId ) UIR inner join Parent PP on
UIR.UserId =pp.UserId insert into parent values(5,'kk','')--主鍵是自增類型,不需要賦值,后面所以字段可以為空值,但是不能沒有對應(yīng)值insert into parent(UserId ,FatherName ) values(6,'ss') -- 添加某些列,其他補(bǔ)NULLupdate parent set FatherName ='GG',MotherName ='HH' where UserId =6 --修改表delete parent where Userid=5 or userid=6 --刪除表insert into CopyParent(userid,copymothername,copyfathername) select UserId,Mothername,fathername from Parent --表的復(fù)制
轉(zhuǎn)載于:https://www.cnblogs.com/hsha/p/6088112.html
總結(jié)
- 上一篇: BZOJ 3173: [Tjoi2013
- 下一篇: 《数据结构与算法Python语言描述》习