[SQL Serverの]クエリの概要 - 詳細な知識とレビュー

図1に示すように、テーブルからの別名として

select * from classinfo as ui

すべての列のクエリ、列を指定する
と、列の別名には、

select ui.cid from classinfo ui

2、nはいくつかのデータの前にクエリ:上位N列名:最初のn行を表示することを

select top 3 * from classinfo--查询前三行所有列

上位Nパーセント列名:データは正面の数パーセントを表します

select top 5 percent *from classinfo--查询前百分之五的所有列数据

3、ソート:列名1 ASC(昇順)によって順| DESC(降順)、列名1 ASC | DESC ...

select * from Userinfo
order by cid desc ,cname desc--先根据cis降序排,再根据cname升序排

図4に示すように、重複する行を排除する:異なります

select distinct cname from classinfo--消除classinfo里的cname相同的(只是查询时消除,并不是删除数据库里的)

5、条件クエリ:どこ背中に書かれました

フィルタ列、bool型の戻り値、行の条件を満たすの列データと、結果セットが結果セットに追加し、またはされていない
比較演算子:=、>、> =、<、<=、= ,!または<>

select * from classinfo
where cid>3

間...そして...連続した範囲を表し、

select * from classinfo
where cid between 2 and 4

これは、の範囲内で非連続を表し、

select * from classinfo
where cid in(3,5)

論理演算子:AND、OR、NOT

select * from classinfo
where cid=3 or cid=5

select * from classinfo
where not cid>=2 and cid<=5

6、ファジークエリ:string型の値を処理する手段、オペレータがあっ%よう_ [] ^

%番号

select * from studentinfo
where sphone like '%4%'--电话号中含4

_シングル

select * from studentinfo
where sname like '虎_'--虎某某

[]範囲
[]内は、連続した範囲を使用することができる-を表します

select * from studentinfo
where sphone like '1[3-5]%'--电话号1开头第二个数字时3到5范围内的电话号

%と_ []で書かれては意味自体を示す
内部を使用していない任意の文字を表し、内部の先頭に[]で書かれました^

select * from studentinfo
where sphone like '1[^553]%'--电话号1开头接着非553内的电话号

。7、ヌルの判断:使用がnullであるか、nullでない他の値を計算するときは、nullが返されますが最低限のヌルを発注する場合に考慮されています

select * from studentinfo
where sphone is null--查询电话是空的

条件に関連付けられたテーブルを結合:に接続された8、

EN:内部ジョイン、両方のテーブル内のデータは完全に一致
外側コネクタを左:外側の左二つのテーブルが正確にデータを一致させる、ジョイン、左テーブルデータ一意
右外側接続:右の外部結合、二つのテーブルが正確なデータと一致、テーブルの右固有のデータが
完全外部ジョイン:完全外部は、テーブルデータ内の2個の完全一致、左に固有のテーブルデータ、右テーブル固有のデータを結合します

select * 
from studentinfo
inner join classinfo on studentinfo.cid=classinfo.cid 

select studentinfo.sname,classinfo.cname
from studentinfo
inner join classinfo on studentinfo.cid=classinfo.cid 

select so.sname,co.cname--建议使用别名
from studentinfo as so
inner join classinfo as co on so.cid=co.cid 

select so.sname,co.cname
from studentinfo as so
right join classinfo as co on so.cid=co.cid 

select so.sname,co.cname
from studentinfo as so
left join classinfo as co on so.cid=co.cid 

select *
from studentinfo as so
full join classinfo as co on so.cid=co.cid 

マルチテーブルクエリ:

select st.sname,cl.cname,sc.score,su.sucourse
from sc
inner join  studentinfo as st on sc.cid=st.cid
inner join  subjectinfo as su on sc.sid=su.suid
inner join  classinfo as cl on st.cid=cl.cid

9、集約関数:合成データの行

合計、平均、カウント、最大値、 minは
、デジタル型の列操作の一般的
クエリは同時に複数の集約関数を書くことができるが、通常書き込み用カラムと混合することができない
計算に関与していない:ヌル問題重合

select count(*) from studentinfo

select sum(score) 
from sc
inner join subjectinfo as su on sc.sid=su.suid
where su.sucourse='语文' 

select avg(score) 
from sc
inner join subjectinfo as su on sc.sid=su.suid
where su.sucourse='语文' 

select max(score) 
from sc
inner join subjectinfo as su on sc.sid=su.suid
where su.sucourse='语文' 

select min(score) 
from sc
inner join subjectinfo as su on sc.sid=su.suid
where su.sucourse='语文' 



10、窓関数:上()

元の表の各行にアウトデータの統計的分布
連動アグリゲーション機能、ランキング機能使用

select sc.*,avg(score) over()
from sc
where sid=1

11、グループ:グループ1人のカラム名で、列名2 ...

関数は、一般的重合パケットと一緒に使用され、パケット統計情報内のデータは、
指定された列に応じてグループ化されています

select COUNT(*)
from studentinfo
group by sgender--计算男女分别总数是多少
select studentinfo.sgender ,studentinfo.cid ,classinfo.cname,COUNT(*) 
from studentinfo
inner join classinfo on classinfo.cid=studentinfo.cid
group by studentinfo.sgender,studentinfo.cid,classinfo.cname
--求每班男女人数且显示班级(先分男女,再分班级)

注:だけバックオフして、グループに戻って選択

--统计学生编号大于2各班级的各性别的学生人数
select sgender,cid,COUNT(*)
from studentinfo
where sid>2
group by sgender,cid

グループ化の基準をスクリーニングした後:持ちます...

--统计学生编号大于2各班级的各性别的学生人数大于1的信息
select sgender,cid,COUNT(*)
from studentinfo
where sid>2
group by sgender,cid having count(*)>1
公開された44元の記事 ウォンの賞賛4 ビュー1041

おすすめ

転載: blog.csdn.net/qq_44162236/article/details/104749144