SQL数据库学习之路(五)
一、查詢
? ?1.為表起別名as。? ?
select *from ClassId as id? --選擇ClassId 表,起別名為id(一般因為原來名字太長所以起短一點的別名)
? ? ??
? ?2.查詢全部列,指定列。為列起別名as。? ??
? ? ? ?select ClassName as 姓名 from ClassId id? ? --選擇ClassId表中的ClassName列,結果窗口只會顯示該列.可以為該列起中文別名。
?
?
?
? ?3.查詢前N部分數據:
? ? ? ?top n 列名:表示查看前n行
? ? ??select top 1 * from gg ? --查詢gg表第一行的數據
?
? ?? ? top n percent 列名:表示查看前百分之幾的數據。
? ? ? ?select top 50 percent ?* from gg ? --查詢gg表前50%的數據
? ?
?
? ?4.排序:order by 列名1 asc|desc,列名1 asc(由小大大)|desc(由大到小)
select *from gg order by Id desc ?--將gg表由Id列由大到小排序
5.消除重復行: distinct
select distinct Id from gg ? --消除gg表中Id重復的行
6.條件查詢:寫在where之后。
? 對行進行篩選,返回bool類型的值。
比較運算符:=,>,>=,<,<=,!=,<>
between...and ...表示在一定的范圍之內。? ? ? ? ? ? ? ? ? ?in表示在一個非連續的范圍內。
邏輯運算符:and? ?or? ?not?
select *from gg where Id=4 ?--查詢gg表中Id列=4的行
select *from gg where Id between 2 and 3 ? --查詢gg表中Id為2和3之間的行
select *from gg where Id in(1,5) ? --查詢gg表中Id為1或5的行
select *from gg where not Id=3 ? ?--查詢gg表中Id不為3的行
7.模糊查詢:用于處理字符串類型的值
運算符包括:like? ?? ?% (表示0到多個)? _(表示一個任意字符)? ? [] ^? ? ? ? ? ? ? ? ? ? ? ?
% 與_寫在[]中表示本身的意思
在[]中表示一個連續的范圍可以使用 -
^寫在[]內部的開頭,表示不使用內部的任何字符
null的判斷:使用is null或者is not null
例子:select *from gg where Name like '%青%' ? ---模糊查詢,名字里面包含青的
? ? ? ? ??select *from gg where Name like '_虎' ? ---模糊查詢,名字里面第二個字是虎的
? ? ? ? ??update gg set Name =null where Id=2 ? ?--將Id為2的名字設置為空
?
?
轉載于:https://www.cnblogs.com/951201193-wzc/p/10294964.html
總結
以上是生活随笔為你收集整理的SQL数据库学习之路(五)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF 3D中多个模型如何设置某一个在最
- 下一篇: PostgreSQL的实践一:初识