MySQL基本查詢實戰
阿新 • • 發佈:2020-12-27
1)建立測試所用表
create table fruits ( f_id char(10) not null, s_id int not null, f_name char(255) not null, f_price decimal(8,2) not null, primary key(f_id) ); #建立測試所用表一 insert into fruits values ('a1',101,'apple','5.2'), ('b1',101,'blackberry','10.2'), ('bs1',102,'orange','11.2'), ('bs2',105,'melon','8.2'), ('t1',102,'banana','10.3'), ('t2',102,'grape','5.3'), ('o2',103,'coconut','9.2'), ('c0',101,'cherry','3.2'), ('a2',103,'apricot','2.2'), ('l2',104,'lemon','6.4'), ('b2',104,'berry','7.6'), ('m1',106,'mango','15.7'), ('m2',105,'xbabay','2.6'), ('t4',107,'xbababa','2.6'), ('m3',105,'xxtt','11.6'), ('b5',107,'xxxx','3.6'); #向表一批量插入資料 create table customers ( c_id int not null auto_increment, c_name char(50) not null, c_address char(50) null, c_city char(50) null, c_zip char(50) null, c_contact char(50) null, c_email char(50) null, primary key(c_id) ); #建立測試所用表二 insert into customers values (10001,'RedHook','200 Street','Tianjin','300000','LiMing','[email protected]'), (1002,'Stars','333 Fromage Lane','Dalian','116000','Zhangbo','[email protected]'), (10003,'Netbhood','1 Sunny Place','Qingdao','266000','LuoCong',NULL), (1004,'JOTO','829 Riverside Drive','Haikou','570000','YangShan','[email protected]'); #向表二批量插入資料
2)檢視兩個表中儲存的資料
select * from fruits; +------+------+------------+---------+ | f_id | s_id | f_name | f_price | +------+------+------------+---------+ | a1 | 101 | apple | 5.20 | | a2 | 103 | apricot | 2.20 | | b1 | 101 | blackberry | 10.20 | | b2 | 104 | berry | 7.60 | | b5 | 107 | xxxx | 3.60 | | bs1 | 102 | orange | 11.20 | | bs2 | 105 | melon | 8.20 | | c0 | 101 | cherry | 3.20 | | l2 | 104 | lemon | 6.40 | | m1 | 106 | mango | 15.70 | | m2 | 105 | xbabay | 2.60 | | m3 | 105 | xxtt | 11.60 | | o2 | 103 | coconut | 9.20 | | t1 | 102 | banana | 10.30 | | t2 | 102 | grape | 5.30 | | t4 | 107 | xbababa | 2.60 | +------+------+------------+---------+ select * from customers; +-------+----------+---------------------+---------+--------+-----------+-------------------+ | c_id | c_name | c_address | c_city | c_zip | c_contact | c_email | +-------+----------+---------------------+---------+--------+-----------+-------------------+ | 1002 | Stars | 333 Fromage Lane | Dalian | 116000 | Zhangbo | [email protected] | | 1004 | JOTO | 829 Riverside Drive | Haikou | 570000 | YangShan | [email protected] | | 10001 | RedHook | 200 Street | Tianjin | 300000 | LiMing | [email protected] | | 10003 | Netbhood | 1 Sunny Place | Qingdao | 266000 | LuoCong | NULL | +-------+----------+---------------------+---------+--------+-----------+-------------------+
3)只查詢fruits表中f_name列的資料
select f_name from fruits; +------------+ | f_name | +------------+ | apple | | apricot | | blackberry | | berry | | xxxx | | orange | | melon | | cherry | | lemon | | mango | | xbabay | | xxtt | | coconut | | banana | | grape | | xbababa | +------------+
4)只查詢fruits表中f_name列和f_price列的資料
select f_name,f_price from fruits;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| blackberry | 10.20 |
| berry | 7.60 |
| xxxx | 3.60 |
| orange | 11.20 |
| melon | 8.20 |
| cherry | 3.20 |
| lemon | 6.40 |
| mango | 15.70 |
| xbabay | 2.60 |
| xxtt | 11.60 |
| coconut | 9.20 |
| banana | 10.30 |
| grape | 5.30 |
| xbababa | 2.60 |
+------------+---------+
5)查詢fruits表中f_name和f_price的列,並且f_price的數值等於5.2
select f_name,f_price from fruits where f_price=5.2;
+--------+---------+
| f_name | f_price |
+--------+---------+
| apple | 5.20 |
+--------+---------+
6)查詢fruits表中f_name和f_price的列,並且f_price的數值大於或等於10
select f_name,f_price from fruits where f_price>=10;
+------------+---------+
| f_name | f_price |
+------------+---------+
| blackberry | 10.20 |
| orange | 11.20 |
| mango | 15.70 |
| xxtt | 11.60 |
| banana | 10.30 |
+------------+---------+
7)查詢fruits表中f_name和f_price的列,並且f_price的數值在2到8之間
select f_name,f_price from fruits where f_price between 2 and 8;
+---------+---------+
| f_name | f_price |
+---------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| berry | 7.60 |
| xxxx | 3.60 |
| cherry | 3.20 |
| lemon | 6.40 |
| xbabay | 2.60 |
| grape | 5.30 |
| xbababa | 2.60 |
+---------+---------+
8)查詢fruits表中的f_name和s_id列,並且s_id的值為101或者103
方法一:
select f_name,s_id from fruits where s_id = 101 or s_id = 103;
+------------+------+
| f_name | s_id |
+------------+------+
| apple | 101 |
| apricot | 103 |
| blackberry | 101 |
| cherry | 101 |
| coconut | 103 |
+------------+------+
方法二:
select f_name,s_id from fruits where s_id in (101,103);
+------------+------+
| f_name | s_id |
+------------+------+
| apple | 101 |
| apricot | 103 |
| blackberry | 101 |
| cherry | 101 |
| coconut | 103 |
+------------+------+
9)查詢fruits表中的f_name和s_id列,並且s_id的值不為101和103
方法一:
select f_name,s_id from fruits where s_id != 101 and s_id != 103;
+---------+------+
| f_name | s_id |
+---------+------+
| berry | 104 |
| xxxx | 107 |
| orange | 102 |
| melon | 105 |
| lemon | 104 |
| mango | 106 |
| xbabay | 105 |
| xxtt | 105 |
| banana | 102 |
| grape | 102 |
| xbababa | 107 |
+---------+------+
方法二:
select f_name,s_id from fruits where s_id not in (101,103);
+---------+------+
| f_name | s_id |
+---------+------+
| berry | 104 |
| xxxx | 107 |
| orange | 102 |
| melon | 105 |
| lemon | 104 |
| mango | 106 |
| xbabay | 105 |
| xxtt | 105 |
| banana | 102 |
| grape | 102 |
| xbababa | 107 |
+---------+------+
10)模糊查詢“%”和“_”的使用
① 查詢fruits表中的f_name列,並且值以“b”開頭
select f_name from fruits where f_name like 'b%';
+------------+
| f_name |
+------------+
| blackberry |
| berry |
| banana |
+------------+
② 查詢fruits表中的f_name列,並且值以“b”開頭,以“y”結尾
select f_name from fruits where f_name like 'b%y';
+------------+
| f_name |
+------------+
| blackberry |
| berry |
+------------+
③ 查詢fruits表中的f_name列,值以“b”開頭,以“y”結尾,並且b和y之間有三個字元
select f_name from fruits where f_name like 'b___y';
+--------+
| f_name |
+--------+
| berry |
+--------+
11)查詢fruits表中s_id的值為101並且f_price的值大於2.0的行
select * from fruits where s_id = 101 and f_price > 2.0;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| c0 | 101 | cherry | 3.20 |
+------+------+------------+---------+
12)查詢fruits表中s_id的值為101或103並且f_price列的值大於5
select * from fruits where s_id in (101,103) and f_price > 5;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| o2 | 103 | coconut | 9.20 |
+------+------+------------+---------+
13)查詢fruits表中的s_id列,並去除重複值
select distinct s_id from fruits;
+------+
| s_id |
+------+
| 101 |
| 103 |
| 104 |
| 107 |
| 102 |
| 105 |
| 106 |
+------+
14)查詢fruits表中的s_id和f_name列,並以s_id對結果進行排序
select s_id,f_name from fruits order by s_id;
+------+------------+
| s_id | f_name |
+------+------------+
| 101 | apple |
| 101 | blackberry |
| 101 | cherry |
| 102 | orange |
| 102 | banana |
| 102 | grape |
| 103 | apricot |
| 103 | coconut |
| 104 | berry |
| 104 | lemon |
| 105 | melon |
| 105 | xbabay |
| 105 | xxtt |
| 106 | mango |
| 107 | xxxx |
| 107 | xbababa |
+------+------------+
15)查詢fruits表中的f_name及f_price列,並以f_name和f_price列進行排序
select distinct f_name,f_price from fruits order by f_name,f_price;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| banana | 10.30 |
| berry | 7.60 |
| blackberry | 10.20 |
| cherry | 3.20 |
| coconut | 9.20 |
| grape | 5.30 |
| lemon | 6.40 |
| mango | 15.70 |
| melon | 8.20 |
| orange | 11.20 |
| xbababa | 2.60 |
| xbabay | 2.60 |
| xxtt | 11.60 |
| xxxx | 3.60 |
+------------+---------+
注:多欄位排序,如果第一個排序的欄位一致,會依靠第二個欄位排序,依次類推,如果第一個欄位不一樣,則直接以第一段來進行排序!
16)查詢fruits表中的f_price列,並對結果以降序進行排序
預設是asc升序排序,可以通過關鍵字DESC更改為降序!
select f_price from fruits order by f_price desc;
+---------+
| f_price |
+---------+
| 15.70 |
| 11.60 |
| 11.20 |
| 10.30 |
| 10.20 |
| 9.20 |
| 8.20 |
| 7.60 |
| 6.40 |
| 5.30 |
| 5.20 |
| 3.60 |
| 3.20 |
| 2.60 |
| 2.60 |
| 2.20 |
+---------+
17)查詢fruits中s_id列不同值出現的次數,並對其進行分組顯示
呼叫count(*)函式統計次數,並通過as來對其設定別名,group by來進行分組!
select s_id,count(*) as total from fruits group by s_id;
+------+-------+
| s_id | total |
+------+-------+
| 101 | 3 |
| 102 | 3 |
| 103 | 2 |
| 104 | 2 |
| 105 | 3 |
| 106 | 1 |
| 107 | 2 |
+------+-------+
18)查詢fruits表中每個相同的s_id對應的f_name列的所有值,f_name的值以一行顯示,並且其值在1個以上
select s_id,group_concat(f_name) as name from fruits group by s_id having count(f_name) > 1;
+------+-------------------------+
| s_id | name |
+------+-------------------------+
| 101 | apple,blackberry,cherry |
| 102 | orange,banana,grape |
| 103 | apricot,coconut |
| 104 | berry,lemon |
| 105 | melon,xbabay,xxtt |
| 107 | xxxx,xbababa |
+------+-------------------------+
19)查詢customers表中c_email列為空值的行
select * from customers where c_email is null;
+-------+----------+---------------+---------+--------+-----------+---------+
| c_id | c_name | c_address | c_city | c_zip | c_contact | c_email |
+-------+----------+---------------+---------+--------+-----------+---------+
| 10003 | Netbhood | 1 Sunny Place | Qingdao | 266000 | LuoCong | NULL |
+-------+----------+---------------+---------+--------+-----------+---------+
20)查詢fruits表中每個s_id對應的所有f_name值
select s_id,group_concat(f_name) as name from fruits group by s_id having count(f_name) > 1;
+------+-------------------------+
| s_id | name |
+------+-------------------------+
| 101 | apple,blackberry,cherry |
| 102 | orange,banana,grape |
| 103 | apricot,coconut |
| 104 | berry,lemon |
| 105 | melon,xbabay,xxtt |
| 107 | xxxx,xbababa |
+------+-------------------------+
21)統計fruits表相同s_id值的行有多少?
select s_id,count(*) as total from fruits group by s_id with rollup;
+------+-------+
| s_id | total |
+------+-------+
| 101 | 3 |
| 102 | 3 |
| 103 | 2 |
| 104 | 2 |
| 105 | 3 |
| 106 | 1 |
| 107 | 2 |
| NULL | 16 |
+------+-------+
22)建立所需表並插入資料
create table orderitems (
o_num int not null,
o_item int not null,
f_id char(10) not null,
quantity int not null,
item_price decimal(8,2) not null,
primary key(o_num,o_item) );
#建立新的測試表
insert into orderitems values
(30001,1,'a1',10,'5.2'),
(30001,2,'b2',3,'7.6'),
(30001,3,'bs1',5,'11.2'),
(30001,4,'bs2',15,'9.2'),
(30002,1,'b3',2,'20.0'),
(30003,1,'c0',100,10),
(30004,1,'o2',50,'2.50'),
(30005,1,'c0',5,'10'),
(30005,2,'b1',10,'8.99'),
(30005,3,'a2',10,'2.2'),
(30005,4,'m1',5,'14.99');
#插入資料
select * from orderitems;
+-------+--------+------+----------+------------+
| o_num | o_item | f_id | quantity | item_price |
+-------+--------+------+----------+------------+
| 30001 | 1 | a1 | 10 | 5.20 |
| 30001 | 2 | b2 | 3 | 7.60 |
| 30001 | 3 | bs1 | 5 | 11.20 |
| 30001 | 4 | bs2 | 15 | 9.20 |
| 30002 | 1 | b3 | 2 | 20.00 |
| 30003 | 1 | c0 | 100 | 10.00 |
| 30004 | 1 | o2 | 50 | 2.50 |
| 30005 | 1 | c0 | 5 | 10.00 |
| 30005 | 2 | b1 | 10 | 8.99 |
| 30005 | 3 | a2 | 10 | 2.20 |
| 30005 | 4 | m1 | 5 | 14.99 |
+-------+--------+------+----------+------------+
#查詢資料
23) 查詢同一個o_num列的quantity(數量)和item_price(價格)相乘結果大於100的行
select o_num,SUM(quantity*item_price) as total from orderitems
group by o_num having total > 100 order by total;
+-------+---------+
| o_num | total |
+-------+---------+
| 30004 | 125.00 |
| 30005 | 236.85 |
| 30001 | 268.80 |
| 30003 | 1000.00 |
+-------+---------+
24)limit——限制返回的行數
① 僅顯示前四行
select * from fruits limit 4;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| a2 | 103 | apricot | 2.20 |
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
+------+------+------------+---------+
② 從第四行開始,顯示後三行
select * from fruits limit 4,3;
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b5 | 107 | xxxx | 3.60 |
| bs1 | 102 | orange | 11.20 |
| bs2 | 105 | melon | 8.20 |
+------+------+--------+---------+
25)查詢每個o_num對應的f_id有幾個
select o_num,count(f_id) as items_total from orderitems group by o_num;
+-------+-------------+
| o_num | items_total |
+-------+-------------+
| 30001 | 4 |
| 30002 | 1 |
| 30003 | 1 |
| 30004 | 1 |
| 30005 | 4 |
+-------+-------------+
26)查詢o_num為30005的quantity(數量)有多少
select sum(quantity) as items_total from orderitems where o_num = 30005;
+-------------+
| items_total |
+-------------+
| 30 |
+-------------+
27)查詢s_id為103的f_price的平均數是多少(s_id的平均價格是多少)
select avg(f_price) as avg_price from fruits where s_id = 103;
+-----------+
| avg_price |
+-----------+
| 5.700000 |
+-----------+
28)查詢每個s_id對應的平均價格(f_price)是多少?
select s_id,avg(f_price) as avg_price from fruits group by s_id;
+------+-----------+
| s_id | avg_price |
+------+-----------+
| 101 | 6.200000 |
| 102 | 8.933333 |
| 103 | 5.700000 |
| 104 | 7.000000 |
| 105 | 7.466667 |
| 106 | 15.700000 |
| 107 | 3.100000 |
+------+-----------+
29)查詢每個s_id中f_price值最大的行是哪個?
select s_id, max(f_price) as max_price from fruits group by s_id;
+------+-----------+
| s_id | max_price |
+------+-----------+
| 101 | 10.20 |
| 102 | 11.20 |
| 103 | 9.20 |
| 104 | 7.60 |
| 105 | 11.60 |
| 106 | 15.70 |
| 107 | 3.60 |
+------+-----------+
#若要檢視最小的行,只需要將max換為min即可。
30)查詢每個f_price值最大的值及其所對應的s_id、f_name
select s_id,f_price,f_name from fruits
where f_price in(select max(f_price) from fruits group by s_id);
+------+---------+------------+
| s_id | f_price | f_name |
+------+---------+------------+
| 101 | 10.20 | blackberry |
| 104 | 7.60 | berry |
| 107 | 3.60 | xxxx |
| 102 | 11.20 | orange |
| 106 | 15.70 | mango |
| 105 | 11.60 | xxtt |
| 103 | 9.20 | coconut |
+------+---------+------------+
31)建立所需表並插入資料
create table suppliers (
s_id int not null auto_increment,
s_name char(50) not null,
s_city char(50) null,
s_zip char(10) null,
s_call char(50) not null,
primary key(s_id) );
#建立測試表一
create table orders (
o_num int not null auto_increment,
o_date datetime not null,
c_id int not null,
primary key(o_num) );
#建立測試表二
insert into suppliers values
(101,'FastFruit Inc.','tianjin','300000','48075'),
(102,'LT Supplies','chongqing','400000','44333'),
(103,'acme','shanghai','200000','90046'),
(104,'fnk inc.','zhongshan','528437','11111'),
(105,'good set','taivuang','030000','22222'),
(106,'just eat ours','beijing','010','45678'),
(107,'dk inc.','zhengzhou','450000','33332');
#向表一插入資料
insert into orders values
(30001,'2008-09-01',10001),
(30002,'2008-09-12',10003),
(30003,'2008-09-30',10004),
(30004,'2008-10-03',10005),
(30005,'2008-10-08',10001);
#向表二插入資料
32)表聯接型別
在進行接下來的查詢,有必要說一下多表查詢的相關概念!
① 內聯接
內聯接(inner join)是最常見的一種聯接方式,只返回兩個資料集合之間匹配關係的行,將位於兩個互相交叉的資料集合中重疊部分以內的資料行聯接起來。
內聯接使用比較運算子進行表間某些列資料的比較操作,並列出這些表中與聯接相匹配的資料行。
② 外聯接
外聯接(outer join)是對內聯接的擴充,除了將兩個資料集合中重複部分以內的資料行聯接起來之外,還可以根據要求返回左側或右側表中非匹配的資料或全部的資料。
外聯接還可以分為以下幾種:
-
左外聯接(left join或left outer join)的結果包括左表的所有行,如果左表的某一行在右表中沒有匹配行,則右表返回空值,否則返回相應值;
-
右外聯接(right join或right outer join)是左外聯接的反向聯接,將返回右表的所有行,如果右表的某一行在左表中沒有匹配行,則左表返回空值,否則返回相應值;
-
全聯接(full join 或full outer join)將返回左表和右表中的所有行,當某一行在另一個表中沒有匹配行時,另一個表返回空值,否則返回相應值;
33)內聯接查詢,將兩個表的指定列生成一個新表
select suppliers.s_id,s_name,f_name,f_price from fruits inner join suppliers on fruits.s_id = suppliers.s_id;
+------+----------------+------------+---------+
| s_id | s_name | f_name | f_price |
+------+----------------+------------+---------+
| 101 | FastFruit Inc. | apple | 5.20 |
| 103 | acme | apricot | 2.20 |
| 101 | FastFruit Inc. | blackberry | 10.20 |
| 104 | fnk inc. | berry | 7.60 |
| 107 | dk inc. | xxxx | 3.60 |
| 102 | LT Supplies | orange | 11.20 |
| 105 | good set | melon | 8.20 |
| 101 | FastFruit Inc. | cherry | 3.20 |
| 104 | fnk inc. | lemon | 6.40 |
| 106 | just eat ours | mango | 15.70 |
| 105 | good set | xbabay | 2.60 |
| 105 | good set | xxtt | 11.60 |
| 103 | acme | coconut | 9.20 |
| 102 | LT Supplies | banana | 10.30 |
| 102 | LT Supplies | grape | 5.30 |
| 107 | dk inc. | xbababa | 2.60 |
+------+----------------+------------+---------+
34)左外聯接查詢示例
select customers.c_id,orders.o_num from customers
left outer join orders on customers.c_id = orders.c_id;
+-------+-------+
| c_id | o_num |
+-------+-------+
| 10001 | 30001 |
| 10003 | 30002 |
| 10001 | 30005 |
| 1002 | NULL |
| 1004 | NULL |
+-------+-------+
35)內聯接查詢時指定其他條件
select customers.c_id,orders.o_num from customers inner join orders on customers.c_id = orders.c_id;
+-------+-------+
| c_id | o_num |
+-------+-------+
| 10001 | 30001 |
| 10003 | 30002 |
| 10001 | 30005 |
+-------+-------+