SQL課堂筆記

來源:互聯網
上載者:User

標籤:des   io   資料   for   art   ar   div   時間   

--注釋公司裡一般而是用絕不重複的guid()做主鍵(web項目不常用)如null參與運算,結果都是null在資料庫中建立索引能提高查詢效率,)只在經常要檢索的欄位建立索引) sql查詢null的資料selsct * from table where name is null 查詢年齡介於20到30的資料selsct * from table where age between 20 and 30 查詢年齡是20,22,32,46的資料selsct * from table where age in(20,22,32,46) 查詢年齡大於20的資料。按升序排序  DESC:降序  ASC:升序selsct top 3 * from table where age>20        //先條件後排序,order by必須在where語句之後order by age ASC 根據年齡進行分組,並計算出每個年齡的人數select age ,count(*) from tablegroup by age  查詢出來的資料根據年齡進行分組,並計算出每個年齡的人數,並計算出同年齡的平均工資,只顯示同年齡人數大於1的select age ,avg (工資),count(*) from tablegroup by agehaving count(*)>1    //having對分組後的資訊過濾,只能用select中的列 子查詢select top 3 * from tablewhere 主鍵 not in (select top 5 工資 from table order by 工資 desc)order by 工資 desc 修改表結構 增加欄位alter table 表名 add 列名 資料類型 去重複查詢select tistinct 工資,姓名 from table 針對顯示的整行一樣則不重複  union 將兩個查詢結果合并到一起,上下兩個資料類型要相容,預設去掉重複資料select 姓名,年齡 from table1union )這裡加all則不去重複,顯示全部)select 姓名,年齡 from table2)可多行合并) sql 函數 ABS():求絕對值CEILING))舍入到最大整數floor))舍入到最小整數round)要修改的數,取小數點後幾位)四捨五入len)計算字串長度)lower()upper()裝換大小寫ltrim))字串左空格去掉rtrim))字串右空格去掉,去兩邊則嵌套兩函數substring()(字串,從哪截取,截取長度是) 轉換類型函數cast(‘123‘ as int)convert(datetime.‘2012-01-01‘)select isnull(欄位,‘名字‘) from table   --將欄位中的null換成指定名字 日期函數getdate))取當前日期dateadd)單位,數量,待計算日期) dateadd)month,-8,date)為計算日期date的8個月之前的日期datediff(單位,開始日期,結束日期)計算兩個日期之間的差額datepart)單位,日期)返回日期的特定部分統計每年入職的員工人數select datepart)year,入職時間) count)*)from 表group by datepart)year,入職時間) case 函數用法:select 名字)case 編號when 1 then ‘使用者‘when 2 then ‘會員‘else ‘未知’end) as 客戶類型from 表名  select o.no,c.name,c.ageform Table1 as o join Table2 as c on o.id=c.idwhere c.age>=20; 子查詢 --單列子查詢select * from tablewhere age>(select min(age) from table)   sql分頁select * from(select row_number() over(order by Fsalary desc) as rownum,* form table) as e1where e1.rownum>=3 and e1.rownum<=5   【SQL 經典習題】  表中有ABC三列,用SQL語句實現:當A列大於B列時選擇A列否則選擇B列,當B列大於C列是選擇B列否則選擇C列。select)case when a>b a else b end),(case when b>c b else c end)from table 在列“客戶類型”中顯示,當編號等於1的時候顯示‘使用者’,當編號等於2的時候顯示‘會員’,其他編號顯示‘未知’select name)case when 工資<2000 then ‘低收入‘when 工資>2000 and 工資<5000 then ‘中等收入‘else ‘高收入’end)from table  --查詢通話時間長度前五條記錄select top 5 * from tableorder by DateDiff(second,StartDateTime,EndDateTime) desc --輸出所有資料中撥打長途號碼)0開頭)的總時間長度select sum(datediff(second,StartDateTime,EndDateTime)) from tablewhere tellnum like ‘0%‘ --輸出本月通話總時間長度最多的前三名呼叫員編號select top 3 sum(second,StartDateTime,EndDateTime)callernumber from tablewhere datediff(month,StartDateTime,getdate  )=0;group by CallerNumberorder by sum(datediff(second,StartDateTime,EndDateTime)) desc --輸出本月撥打到電話次數最多的前三個呼叫員的編號select top 3 CallerNumber from tablewhere datediff(month,StartDateTime,getdate  )=0;group by CallerNumberorder by Count(*) desc --輸出所有資料的撥號流水)呼叫員編號,對方號碼,通話時間長度),並且在最後一行添加總呼叫時間長度,匯總【市總】【長總】 select CallerNumber,tellnum,DateDiff(second,StartDateTime,EndDateTime)form tableunion allselect N‘匯總‘,Convert(varchar(50),sum((case when tellnum like ‘0%‘ then DateDiff(second,StartDateTime,EndDateTime)else 0end))),sum((case when tellnum not like ‘0%‘ then DateDiff(second,StartDateTime,EndDateTime)else 0end))from table

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.