欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

必須會的SQL語句(六) 數(shù)據(jù)查詢

 更新時間:2015年01月03日 01:18:27   投稿:mdxy-dxy  
這篇文章主要介紹了sqlserver中數(shù)據(jù)查詢方法,需要的朋友可以參考下

1.基礎(chǔ)的查詢
    1)重命名列
    select name as '姓名' from 表名
 
    2)定義常量列
    select 是否 ='是' from 表名
 
    3)top用法 percent
     --這種寫法可以獲取前20%條字段。
      select top 20 percent * from 表名
 
    4)去除重復(fù)列
     select distinct 列名 from 表名
   
    5)聚合函數(shù)
     max    avg    count    min    sum
     --多個聚合結(jié)果 在一個結(jié)果集中
     select
        最大年齡 = (select max(age) from 表名),
        最小年齡 = (select min(age) from 表名)
 
    6)between and
        select * from 表 where xx  between 5 and 6
    
2.Union 使用Union將兩個結(jié)果集匯聚在一起。
 --     年齡      工資
-- ————————
--      19       $20000
--      50       $20005
--      30       $23000
--     匯總     $63005

--   查詢各年齡段工資,同時顯示所有工資匯總。(像上邊的表)
select
--把年齡轉(zhuǎn)換成varchar類型
Convert(varchar(10),[age]) as 年齡
Sum([salary]) as 工資
from  員工表
group by age
--將兩個結(jié)果集,合并成一個結(jié)果集
union
select
--匯總是一個常量列
'匯總' , sum(salary)
from 員工表
     使用union合并兩個結(jié)果集時,
     兩個結(jié)果集列數(shù)必須一致,并且數(shù)據(jù)類型對應(yīng)。
     這就是代碼中,把年齡轉(zhuǎn)換成varchar的原因。
 
3.Order by
  -- Order by 用于結(jié)果集排序,
  -- 其Order他后邊不只可以接一個字段,
  -- 也能接一個 表達(dá)式。
Select *
    from 表
    order by (age+salary)/2.0 desc

相關(guān)文章

最新評論