sql的基本查詢語句


--------------------------------------------基本常用查詢--------------------------------------

自己簡單練習做了個表。今天看了下hoojo大神的SQL Server T-SQL高級查詢,我現在程度只能理解基礎,所以就分享一下自己所聯系的和理解的部分。

--查詢所有 
select * from [dbo].[Table]
--查詢出所有名字
select all name from [dbo].[Table]
--過濾掉重復的性別
select distinct sex from [dbo].[Table]
--統計出表中有多少條信息
select count(*) from [dbo].[Table]
--統計出表中所有名字有多少條信息
select count(name) from [dbo].[Table]
--統計出過濾掉重復性別有多少條信息
select COUNT(distinct sex) from [dbo].[Table]
--排行前5名的姓名
select top 5 name from [dbo].[Table]
--如果要知道所有信息就將name替換成*----------------------
--列重命名(三種方式空格,as,單引號) --常用的是空格
select id 編號 , name as 姓名 , sex '性別' from [dbo].[Table]
--表重命名(倆種方式空格,as) --常用的是空格
select * from [dbo].[Table] as t
select * from [dbo].[Table] t
--列運算(倆列相加)
select (id+age) 總數 from [Table]
--將倆列合並顯示在一列中用-隔開
select name +'-'+ age 個人信息 from [Table]
--where條件 (后面接列名 運算符 值)
select id 編號, name 姓名 from [Table] where id =2
select * from [Table] where id <=8
select * from [Table] where id >=2 
select * from [Table] where id !=3
select * from [Table] where id !<7 
select * from [Table] where id !>8
--將名字包含鄭竹的信息不顯示
select * from [Table] where name<>'鄭竹'
--and 和 or 用法
--and 的用法是並且(滿足所有條件)
select * from [Table] where id=3 and age=14 
--or的用法是或者(滿足其中一個條件)
select * from [Table] where age=13 or age=14
--between ...and...用法是倆者之間(顯示條件中倆者之間的所有信息)
select * from [Table] where id between 2 and 8
--模糊查詢(%替換所有輸入字符,_替換一個輸入字符) 關鍵字like替換運算符
select * from [Table] where name like '%'
select * from [Table] where name like '%電'
select * from [Table] where name like '_雨'
--子查詢 關鍵字in (在條件之中)
select * from [Table] where id in (2,3,4,7)
select * from [Table] where name in('錢雨','李電','鄭竹')
--not in (不在條件之中)
select * from [Table] where age not in (17,18,19)
select * from [Table] where name not in('錢雨','李電','鄭竹')
--is null (顯示條件中值為空的信息)
select * from [Table] where age is null 
--is not null (顯示條件中值不為空的信息)
select * from [Table] where age is not null
--order by 排序 (desc 降序,asc升序)
select * from [Table] order by age
select * from [Table] order by age desc 
select * from [Table] order by age asc
--group by 分組 
--(查詢時將一列或n列分開查詢並顯示,查詢條件一一對應后面group by)
select age from [Table] group by age
select id ,name, age from [Table] group by id,name, age
--按照年齡進行分組統計
select count(*) age from [Table] group by age
--按照性別進行分組統計
select count(*) sex from [Table] group by sex
--按照年齡和性別組合分組統計,並排序
select COUNT(*) age,sex from [Table] group by age,sex order by age
--按照性別分組,並且是id大於2的記錄最后按照性別排序
select COUNT(*) id大於2的人數, sex from [Table] where id>2 group by sex order by sex 
--查詢id大於2的數據,並完成運算后的結果進行分組和排序
select COUNT(*) id大於2的人數,(age+id) 合計數 from [Table] where id>2 group by (age+id) order by (age+id)
--group by all 
--按照年齡分組,是所有的年齡
select age from [Table] group by all age
--having 分組過濾條件
--按照年齡分組,過濾年齡為空的數據,並且統計分組的條數和現實年齡信息
select COUNT(*) 分組的條數, age from [Table] group by age having age is not null
--按照年齡和id組合分組,過濾條件是id大於1的記錄
select id,age from [Table] group by id,age having id >1
--按照年齡分組,過濾條件是分組后的記錄條數大於等於1
select COUNT(*) 分組后的記錄條數, age from [Table] group by age having COUNT(*) >=1
--按照id和性別組合分組,過濾條件是id大於1,id的最大值大於2
select id,sex from [Table] group by id,sex having id>1 and max(id)>2

 

--------------------------------------------嵌套子查詢--------------------------------------
--子查詢就是內部查詢或者說是內部選擇,在子查詢外部的是外部查詢或者說是外部選擇。
--這個是基礎理論,我個人覺得子查詢就是中心,外部查詢就是外圍,中心查詢的語句都是基礎語句演變而來,外部查詢就是結合子查詢一塊查詢結果
--下面就是簡單的子查詢格式
select * from /* 這個就是外部查詢*/
(select id ,name ,age from [Table] where id >1) t /*內部查詢*/
where t.age >15/* 這個就是外部查詢*/
--內部查詢中運用的語句就是基本常用查詢語句
--外部查詢可以包含基本常用查詢語句
--例如where,group by,having,count,select查詢,多個表或者視圖的from語句


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM