一、普通查詢
整體遵循以下次序:
1、常用聚合函數如下:
eg1 : 找出表中的最大攻擊力的值?
select max(attack) from sanguo;
eg2 : 表中共有多少個英雄?
select count(name) as number from sanguo;
eg3 : 蜀國英雄中攻擊值大於200的英雄的數量
select count(id) from sanguo where country='蜀國' and attack>200;
2、group by :給查詢的結果進行分組
eg1 : 計算每個國家的平均攻擊力
select country,avg(attack) from sanguo
group by country;
eg2 : 所有國家的男英雄中 英雄數量最多的前2名的 國家名稱及英雄數量
select country,count(id) as number from sanguo
where gender='M' group by country
order by number DESC
limit 2;
注意:group by后字段名必須要為select后的字段,查詢字段和group by后字段不一致,則必須對該字段進行聚合處理(聚合函數)
3、having語句:對分組聚合后的結果進行進一步篩選
eg1 : 找出平均攻擊力大於105的國家的前2名,顯示國家名稱和平均攻擊力
select country,avg(attack) from sanguo
group by country
having avg(attack)>105
order by avg(attack) DESC
limit 2;
注意:
1)having語句通常與group by聯合使用
2)having語句存在彌補了where關鍵字不能與聚合函數聯合使用的不足,where只能操作表中實際存在的字段,having操作的是聚合函數生成的顯示列
4、distinct語句:不顯示字段重復值
eg1 : 表中都有哪些國家
select distinct name,country from sanguo;
eg2 : 計算一共有多少個國家
select count(distinct country) from sanguo;
注意:
1)distinct和from之間所有字段都相同才會去重
2)distinct不能對任何字段做聚合處理
5、查詢表記錄時做數學運算
運算符 : + - * / % **
eg1: 查詢時顯示攻擊力翻倍
select name,attack*2 from sanguo;
eg2: 更新蜀國所有英雄攻擊力 * 2
update sanguo set attack=attack*2 where country='蜀國';
總結:
二、嵌套查詢(子查詢)
1、定義:把內層的查詢結果作為外層的查詢條件
2、語法格式:
select ... from 表名 where 條件(select ....);
3、示例:
1)把攻擊值小於平均攻擊值的英雄名字和攻擊值顯示出來
select name,attack from sanguo where attack<(select avg(attack) from sanguo);
2)找出每個國家攻擊力最高的英雄的名字和攻擊值(子查詢)
select name,attack from sanguo where (country,attack) in(select country,max(attack) from sanguo group by country);
多表查詢
三、
1、笛卡爾積
select 字段名列表 from 表名列表;
多表查詢
2、
select 字段名列表 from 表名列表 where 條件;
示例:
3、
sql腳本如下:
mysql -uroot -p123456
mysql>source /home/tarena/join_query.sql
create database if not exists db1 character set utf8;
use db1;
create table if not exists province(
id int primary key auto_increment,
pid int,
pname varchar(15)
)default charset=utf8;
insert into province values
(1, 130000, '河北省'),
(2, 140000, '陝西省'),
(3, 150000, '四川省'),
(4, 160000, '廣東省'),
(5, 170000, '山東省'),
(6, 180000, '湖北省'),
(7, 190000, '河南省'),
(8, 200000, '海南省'),
(9, 200001, '雲南省'),
(10,200002,'山西省');
create table if not exists city(
id int primary key auto_increment,
cid int,
cname varchar(15),
cp_id int
)default charset=utf8;
insert into city values
(1, 131100, '石家庄市', 130000),
(2, 131101, '滄州市', 130000),
(3, 131102, '廊坊市', 130000),
(4, 131103, '西安市', 140000),
(5, 131104, '成都市', 150000),
(6, 131105, '重慶市', 150000),
(7, 131106, '廣州市', 160000),
(8, 131107, '濟南市', 170000),
(9, 131108, '武漢市', 180000),
(10,131109, '鄭州市', 190000),
(11,131110, '北京市', 320000),
(12,131111, '天津市', 320000),
(13,131112, '上海市', 320000),
(14,131113, '哈爾濱', 320001),
(15,131114, '雄安新區', 320002);
create table if not exists county(
id int primary key auto_increment,
coid int,
coname varchar(15),
copid int
)default charset=utf8;
insert into county values
(1, 132100, '正定縣', 131100),
(2, 132102, '浦東新區', 131112),
(3, 132103, '武昌區', 131108),
(4, 132104, '哈哈', 131115),
(5, 132105, '安新縣', 131114),
(6, 132106, '容城縣', 131114),
(7, 132107, '雄縣', 131114),
(8, 132108, '嘎嘎', 131115);
1、顯示省和市的詳細信息
河北省 石家庄市 河北省 廊坊市 湖北省 武漢市 select province.pname,city.cname from province,city where province.pid=city.cp_id;
連接查詢
2、顯示 省 市 縣 詳細信息 select province.pname,city.cname,county.coname from province,city,county where province.pid=city.cp_id and city.cid=county.copid;
四、
1、內連接(結果同多表查詢,顯示匹配到的記錄)
語法格式:select 字段名 from 表1 inner join 表2 on 條件 inner join 表3 on 條件;
eg1 : 顯示省市詳細信息 select province.pname,city.cname from province inner join city on province.pid=city.cp_id; eg2 : 顯示 省 市 縣 詳細信息 select province.pname,city.cname,county.coname from province inner join city on province.pid=city.cp_id inner join county on city.cid=county.copid;
左外連接:以 左表 為主顯示查詢結果
2、
語法格式:
select 字段名 from 表1 left join 表2 on 條件 left join 表3 on 條件;
eg1 : 顯示 省 市 詳細信息(要求省全部顯示)
select province.pname,city.cname from province
left join city on province.pid=city.cp_id;
右外連接:用法同左連接,以右表為主顯示查詢結果
3、
語法格式:
select 字段名 from 表1 right join 表2 on 條件 right join 表3 on 條件;