關于SQL查詢語句關鍵字方法
SQL常用的一些關鍵字
1、distinct關鍵字
顯示沒有重復記錄的商品名稱,商品價格和商品類別列表
select distinct ware_name,price from t_ware;
2、使用計算列
查詢所有商品價格提高20%后的價格
select ware_id,ware_name,price*1.2 from t_ware'
3、列的別名
a) 不使用as
select ware_id,ware_name,price*1.2 price_raise? from t_ware;
b)使用as
select ware_id,ware_name,price*1.2 price_raise? from t_ware;
4、使用邏輯表達式
a)not 顯示商品價格不大于100的商品
select ware_id,ware_name,price,category_id? from t_ware? where not price>100;
b)or 顯示商品類別編號為5或6或7的商品
select ware_id,ware_name,price,category_id? from t_ware? where category_id=5 or category_id=6? or category_id=7;
c)and 顯示商品價格大于100且商品類別編號為5的商品
select ware_id,ware_name,price,category_id? from t_ware? where not price>100 and category_id = 5;
5、使用between關鍵字
顯示商品價格在200元至1000元之間的商品(留心一下,是半開區(qū)間還是封閉區(qū)間?)
select ware_id,ware_name,price,category_id? from t_ware? where price between 200 and 1000;
6、使用in關鍵字
顯示商品類別為5,6,7且價格不小于200元的商品
select ware_id,ware_name,price,category_id? from t_ware? where category_id in (5,6,7) and price>=200;
7、使用like子句進行模糊查詢
a)%(百分號)表示0到n個任意字符
select ware_id,ware_name,price,category_id? from t_ware? where ware_name like '%純棉%';
b)_(下劃線)表示單個的任意字符
select ware_id,ware_name,price,category_id? from t_ware? where ware_name like ?'%長袖_恤%';
8、轉義字符escape的使用
select ware_id,ware_name,price,category_id? from t_ware? where ware_name like '%\%%' escape '\';
9、使用order by給數據排序
?? ?select * from t_ware_category? ?? ?where parent_id = 0 order by ware_id ; ?? ?-------- ?? ?select * from t_ware_category? ?? ?where parent_id = 0 order by ware_id asc; ?? ?--------- ?? ?select * from t_ware_category ?? ?where parent_id = 0 order by ware_id desc ;
rownum
a)查詢前20條商品記錄
select ware_id,ware_name,price? from t_ware? where rownum <= 20;
b)查詢第11條至第20條記錄
select ware_id,ware_name,price from t_ware? where rownum<=10 and ware_id? not in(select ware_id from t_ware where rownum<=10);
10、常用統(tǒng)計函數
a) sum()返回一個數字列或計算列的總和
select sum(price) from t_ware;
b) avg()對一個數字列或計算列球平均值
c) min()返回一個數字列或一個數字表達式的最小值
d) max()返回一個數字列或一個數字表達式的最大值
e) count()返回滿足select語句中指定的條件的記錄值
11、多表查詢和笛卡爾乘積
查詢商品編號,商品名稱,商品價格和商品類別名稱
select? t_ware.ware_id, t_ware.ware_name, t_ware.price ,t_ware_category_name? from t_ware, t_ware_category ? where t_ware.category_id=t_ware_category.category_id;
使用join
a)左連接
select? t_ware.ware_id,t_ware.ware_name,t_ware.price,t_ware_category.category_name? from t_ware? left join t_ware_category? on t_ware.category_id=t_ware_category.category_id;
select w.ware_id,w.ware_name,w.price,wc.category_name? from t_ware w? left join t_ware_category wc? on w.category_id=wc.category_id;
b) 右連接
select t_ware.ware_id,t_ware.ware_name,t_ware.price,t_ware_category.category_name from t_ware? left join t_ware_category? on t_ware.category_id=t_ware_category.category_id;
12、使用union
select ware_id,ware_name? from t_ware? where ware_name like '%T恤%'? union? select ware_id,ware_name ?from t_ware? ?where ware_name like '%手提包%'
13、使用group by
a)統(tǒng)計每個二級類別下有多少商品,以及商品總價值
select w.category_id,wc.category_name, count(w.ware_id),sum(w.price)? from t_ware w? left join t_ware_category wc? on w.category_id=wc.category_id? group by w.category_id,wc.category_name;
b) 統(tǒng)計每個一級類別下有多少商品,以及商品總價值
select wc2.category_id,wc2.category_name,sum(w.price)? from t_ware w? left join t_ware_category wc? on w.category_id=wc.category_id? left join t_ware_category wc2? on wc.parent_id=wc2.category_id? group by wc2.category_id,wc2.category_name;
14、使用having對結果進行篩選
–舉例子說明:查詢table表查詢每一個班級中年齡大于20,性別為男的人數
select COUNT(*)as '>20歲人數',classid ? from Table1? where sex='男'? group by classid,age having age>20?
需要注意說明:當同時含有where子句、group by 子句 、having子句及聚集函數時,執(zhí)行順序如下:
執(zhí)行where子句查找符合條件的數據;
使用group by 子句對數據進行分組;對group by 子句形成的組運行聚集函數計算每一組的值;最后用having 子句去掉不符合條件的組。
- having 子句中的每一個元素也必須出現(xiàn)在select列表中。有些數據庫例外,如oracle.
- having子句和where子句都可以用來設定限制條件以使查詢結果滿足一定的條件限制。
- having子句限制的是組,而不是行。where子句中不能使用聚集函數,而having子句中可以。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SQL?Server使用xp_readerrorlog命令查看錯誤日志
本文探討了SQL?Server中查看日志的四種方法,重點介紹了xp_readerrorlog命令的使用方法,xp_readerrorlog命令用于T-SQL讀取SQL?Server錯誤日志,有助于對SQL?Server中的問題進行故障排除,避免因特定情況而從GUI讀取大型錯誤日志的麻煩2025-03-03
SQL Server附加數據庫報錯無法打開物理文件,操作系統(tǒng)錯誤5的圖文解決教程
sqlserver附加數據時,提示無法打開物理文件,操作系統(tǒng)錯誤5什么原因呢?今天小編給大家分享SQL Server附加數據庫報錯無法打開物理文件,操作系統(tǒng)錯誤5的圖文解決教程,一起看看吧2016-12-12

