目錄mysql
# 返回表的每個字段 mysql> show columns from customers; +--------------+-----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------+------+-----+---------+----------------+ | cust_id | int(11) | NO | PRI | NULL | auto_increment | | cust_name | char(50) | NO | | NULL | | | cust_address | char(50) | YES | | NULL | | | cust_city | char(50) | YES | | NULL | | | cust_state | char(5) | YES | | NULL | | | cust_zip | char(10) | YES | | NULL | | | cust_country | char(50) | YES | | NULL | | | cust_contact | char(50) | YES | | NULL | | | cust_email | char(255) | YES | | NULL | | +--------------+-----------+------+-----+---------+----------------+ 9 rows in set (0.00 sec) # 還可使用describe語句 mysql> describe customers; +--------------+-----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------+------+-----+---------+----------------+ | cust_id | int(11) | NO | PRI | NULL | auto_increment | | cust_name | char(50) | NO | | NULL | | | cust_address | char(50) | YES | | NULL | | | cust_city | char(50) | YES | | NULL | | | cust_state | char(5) | YES | | NULL | | | cust_zip | char(10) | YES | | NULL | | | cust_country | char(50) | YES | | NULL | | | cust_contact | char(50) | YES | | NULL | | | cust_email | char(255) | YES | | NULL | | +--------------+-----------+------+-----+---------+----------------+ 9 rows in set (0.00 sec) # 顯示建立數據庫的語句 mysql> show create database sys; +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | sys | CREATE DATABASE `sys` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+--------------------------------------------------------------+ 1 row in set (0.00 sec) # 顯示建立數據庫表的語句 mysql> show create table customers; +-----------+----+ | Table | Create Table +-----------+----+ | customers | CREATE TABLE `customers` ( `cust_id` int(11) NOT NULL AUTO_INCREMENT, `cust_name` char(50) NOT NULL, `cust_address` char(50) DEFAULT NULL, `cust_city` char(50) DEFAULT NULL, `cust_state` char(5) DEFAULT NULL, `cust_zip` char(10) DEFAULT NULL, `cust_country` char(50) DEFAULT NULL, `cust_contact` char(50) DEFAULT NULL, `cust_email` char(255) DEFAULT NULL, PRIMARY KEY (`cust_id`) ) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=utf8 +-----------+----+ 1 row in set (0.00 sec)
mysql> select prod_name from products; +----------------+ | prod_name | +----------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | Detonator | | Bird seed | | Carrots | | Fuses | | JetPack 1000 | | JetPack 2000 | | Oil can | | Safe | | Sling | | TNT (1 stick) | | TNT (5 sticks) | +----------------+ 14 rows in set (0.00 sec)
mysql> select prod_id, prod_name, prod_price from products; +---------+----------------+------------+ | prod_id | prod_name | prod_price | +---------+----------------+------------+ | ANV01 | .5 ton anvil | 5.99 | | ANV02 | 1 ton anvil | 9.99 | | ANV03 | 2 ton anvil | 14.99 | | DTNTR | Detonator | 13.00 | | FB | Bird seed | 10.00 | | FC | Carrots | 2.50 | | FU1 | Fuses | 3.42 | | JP1000 | JetPack 1000 | 35.00 | | JP2000 | JetPack 2000 | 55.00 | | OL1 | Oil can | 8.99 | | SAFE | Safe | 50.00 | | SLING | Sling | 4.49 | | TNT1 | TNT (1 stick) | 2.50 | | TNT2 | TNT (5 sticks) | 10.00 | +---------+----------------+------------+ 14 rows in set (0.00 sec)
mysql> select * from products; +---------+---------+----------------+------------+----------------------------------------------------------------+ | prod_id | vend_id | prod_name | prod_price | prod_desc | +---------+---------+----------------+------------+----------------------------------------------------------------+ | ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook | | ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case | | ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case | | DTNTR | 1003 | Detonator | 13.00 | Detonator (plunger powered), fuses not included | | FB | 1003 | Bird seed | 10.00 | Large bag (suitable for road runners) | | FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) | | FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long | | JP1000 | 1005 | JetPack 1000 | 35.00 | JetPack 1000, intended for single use | | JP2000 | 1005 | JetPack 2000 | 55.00 | JetPack 2000, multi-use | | OL1 | 1002 | Oil can | 8.99 | Oil can, red | | SAFE | 1003 | Safe | 50.00 | Safe with combination lock | | SLING | 1003 | Sling | 4.49 | Sling, one size fits all | | TNT1 | 1003 | TNT (1 stick) | 2.50 | TNT, red, single stick | | TNT2 | 1003 | TNT (5 sticks) | 10.00 | TNT, red, pack of 10 sticks | +---------+---------+----------------+------------+----------------------------------------------------------------+ 14 rows in set (0.00 sec)
mysql> select distinct vend_id from products; +---------+ | vend_id | +---------+ | 1001 | | 1002 | | 1003 | | 1005 | +---------+ 4 rows in set (0.00 sec)
# 從第0行開始,顯示3行 mysql> select distinct vend_id from products limit 3; # 或者 select distinct vend_id from products limit 0, 3; # 或者 select distinct vend_id from products limit 3 offset 0; +---------+ | vend_id | +---------+ | 1001 | | 1002 | | 1003 | +---------+ 3 rows in set (0.00 sec)
mysql> select prod_name from products order by prod_name; +----------------+ | prod_name | +----------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | Bird seed | | Carrots | | Detonator | | Fuses | | JetPack 1000 | | JetPack 2000 | | Oil can | | Safe | | Sling | | TNT (1 stick) | | TNT (5 sticks) | +----------------+
mysql> select prod_id, prod_price, prod_name from products order by prod_price, prod_name; +---------+------------+----------------+ | prod_id | prod_price | prod_name | +---------+------------+----------------+ | FC | 2.50 | Carrots | | TNT1 | 2.50 | TNT (1 stick) | | FU1 | 3.42 | Fuses | | SLING | 4.49 | Sling | | ANV01 | 5.99 | .5 ton anvil | | OL1 | 8.99 | Oil can | | ANV02 | 9.99 | 1 ton anvil | | FB | 10.00 | Bird seed | | TNT2 | 10.00 | TNT (5 sticks) | | DTNTR | 13.00 | Detonator | | ANV03 | 14.99 | 2 ton anvil | | JP1000 | 35.00 | JetPack 1000 | | SAFE | 50.00 | Safe | | JP2000 | 55.00 | JetPack 2000 | +---------+------------+----------------+ 14 rows in set (0.00 sec)
mysql> select prod_id, prod_price, prod_name from products order by prod_price desc; +---------+------------+----------------+ | prod_id | prod_price | prod_name | +---------+------------+----------------+ | JP2000 | 55.00 | JetPack 2000 | | SAFE | 50.00 | Safe | | JP1000 | 35.00 | JetPack 1000 | | ANV03 | 14.99 | 2 ton anvil | | DTNTR | 13.00 | Detonator | | FB | 10.00 | Bird seed | | TNT2 | 10.00 | TNT (5 sticks) | | ANV02 | 9.99 | 1 ton anvil | | OL1 | 8.99 | Oil can | | ANV01 | 5.99 | .5 ton anvil | | SLING | 4.49 | Sling | | FU1 | 3.42 | Fuses | | FC | 2.50 | Carrots | | TNT1 | 2.50 | TNT (1 stick) | +---------+------------+----------------+ 14 rows in set (0.00 sec)
mysql> select prod_id, prod_price, prod_name from products order by prod_price desc limit 3 offset 0; +---------+------------+--------------+ | prod_id | prod_price | prod_name | +---------+------------+--------------+ | JP2000 | 55.00 | JetPack 2000 | | SAFE | 50.00 | Safe | | JP1000 | 35.00 | JetPack 1000 | +---------+------------+--------------+ 3 rows in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name='fuses'; +-----------+------------+ | prod_name | prod_price | +-----------+------------+ | Fuses | 3.42 | +-----------+------------+ 1 row in set (0.00 sec)
mysql> select vend_id, prod_name from products where vend_id <> 1003; +---------+--------------+ | vend_id | prod_name | +---------+--------------+ | 1001 | .5 ton anvil | | 1001 | 1 ton anvil | | 1001 | 2 ton anvil | | 1002 | Fuses | | 1005 | JetPack 1000 | | 1005 | JetPack 2000 | | 1002 | Oil can | +---------+--------------+ 7 rows in set (0.00 sec)
mysql> select prod_price, prod_name from products where prod_price between 5 and 10; +------------+----------------+ | prod_price | prod_name | +------------+----------------+ | 5.99 | .5 ton anvil | | 9.99 | 1 ton anvil | | 10.00 | Bird seed | | 8.99 | Oil can | | 10.00 | TNT (5 sticks) | +------------+----------------+ 5 rows in set (0.00 sec)
mysql> select cust_name, cust_email from customers where cust_email is null; +-------------+------------+ | cust_name | cust_email | +-------------+------------+ | Mouse House | NULL | | E Fudd | NULL | +-------------+------------+ 2 rows in set (0.00 sec)
mysql> select prod_name, vend_id, prod_price from products where vend_id = 1003 and prod_price < 5; +---------------+---------+------------+ | prod_name | vend_id | prod_price | +---------------+---------+------------+ | Carrots | 1003 | 2.50 | | Sling | 1003 | 4.49 | | TNT (1 stick) | 1003 | 2.50 | +---------------+---------+------------+ 3 rows in set (0.01 sec)
mysql> select prod_name, vend_id, prod_price from products where vend_id = 1003 or prod_price < 5; +----------------+---------+------------+ | prod_name | vend_id | prod_price | +----------------+---------+------------+ | Detonator | 1003 | 13.00 | | Bird seed | 1003 | 10.00 | | Carrots | 1003 | 2.50 | | Fuses | 1002 | 3.42 | | Safe | 1003 | 50.00 | | Sling | 1003 | 4.49 | | TNT (1 stick) | 1003 | 2.50 | | TNT (5 sticks) | 1003 | 10.00 | +----------------+---------+------------+ 8 rows in set (0.00 sec)
# and 優先級高於or # 找出vend_id爲1002或1003,而且prod_id大於等於10的產品 mysql> select prod_name, vend_id, prod_price from products where (vend_id = 1003 or vend_id = 1002) and prod_price >= 10; +----------------+---------+------------+ | prod_name | vend_id | prod_price | +----------------+---------+------------+ | Detonator | 1003 | 13.00 | | Bird seed | 1003 | 10.00 | | Safe | 1003 | 50.00 | | TNT (5 sticks) | 1003 | 10.00 | +----------------+---------+------------+ 4 rows in set (0.00 sec)
mysql> select prod_name, vend_id, prod_price from products where vend_id in (1002, 1003) order by prod_price desc; +----------------+---------+------------+ | prod_name | vend_id | prod_price | +----------------+---------+------------+ | Safe | 1003 | 50.00 | | Detonator | 1003 | 13.00 | | Bird seed | 1003 | 10.00 | | TNT (5 sticks) | 1003 | 10.00 | | Oil can | 1002 | 8.99 | | Sling | 1003 | 4.49 | | Fuses | 1002 | 3.42 | | Carrots | 1003 | 2.50 | | TNT (1 stick) | 1003 | 2.50 | +----------------+---------+------------+ 9 rows in set (0.00 sec)
mysql> select prod_name, vend_id, prod_price from products where vend_id not in (1002, 1003) order by prod_price desc; +--------------+---------+------------+ | prod_name | vend_id | prod_price | +--------------+---------+------------+ | JetPack 2000 | 1005 | 55.00 | | JetPack 1000 | 1005 | 35.00 | | 2 ton anvil | 1001 | 14.99 | | 1 ton anvil | 1001 | 9.99 | | .5 ton anvil | 1001 | 5.99 | +--------------+---------+------------+ 5 rows in set (0.00 sec)
# %表示任何字符出現任意次數【0、一、多】次 mysql> select prod_name, prod_price from products where prod_name like 'jet%'; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | JetPack 1000 | 35.00 | | JetPack 2000 | 55.00 | +--------------+------------+ 2 rows in set (0.00 sec)
# _通配符和%同樣,只是_只匹配一個字符而不是多個字符 mysql> select prod_id, prod_name, prod_price from products where prod_name like '_ ton anvil'; +---------+-------------+------------+ | prod_id | prod_name | prod_price | +---------+-------------+------------+ | ANV02 | 1 ton anvil | 9.99 | | ANV03 | 2 ton anvil | 14.99 | +---------+-------------+------------+
mysql> select prod_name, prod_price from products where prod_name regexp '1000'; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | JetPack 1000 | 35.00 | +--------------+------------+ 1 row in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '.000' order by prod_price desc; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | JetPack 2000 | 55.00 | | JetPack 1000 | 35.00 | +--------------+------------+ 2 rows in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '1000|2000' order by prod_price desc; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | JetPack 2000 | 55.00 | | JetPack 1000 | 35.00 | +--------------+------------+ 2 rows in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '[123] ton' order by prod_price desc; +-------------+------------+ | prod_name | prod_price | +-------------+------------+ | 2 ton anvil | 14.99 | | 1 ton anvil | 9.99 | +-------------+------------+ 2 rows in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '[1-5] ton' order by prod_price desc; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | 2 ton anvil | 14.99 | | 1 ton anvil | 9.99 | | .5 ton anvil | 5.99 | +--------------+------------+ 3 rows in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '\\.'; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | .5 ton anvil | 5.99 | +--------------+------------+ 1 row in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '\\([0-9] sticks?\\)'; +----------------+------------+ | prod_name | prod_price | +----------------+------------+ | TNT (1 stick) | 2.50 | | TNT (5 sticks) | 10.00 | +----------------+------------+ 2 rows in set (0.00 sec)
mysql> select prod_name, prod_price from products where prod_name regexp '^[0-9\\.]'; +--------------+------------+ | prod_name | prod_price | +--------------+------------+ | .5 ton anvil | 5.99 | | 1 ton anvil | 9.99 | | 2 ton anvil | 14.99 | +--------------+------------+ 3 rows in set (0.00 sec)
mysql> select Concat(vend_name, ' (', vend_country, ')') from vendors order by vend_name; +--------------------------------------------+ | Concat(vend_name, ' (', vend_country, ')') | +--------------------------------------------+ | ACME (USA) | | Anvils R Us (USA) | | Furball Inc. (USA) | | Jet Set (England) | | Jouets Et Ours (France) | | LT Supplies (USA) | +--------------------------------------------+ 6 rows in set (0.00 sec) # Trim函數 MySQL除了支持RTrim()(正如剛纔所見,它去掉 # 串右邊的空格),還支持LTrim()(去掉串左邊的空格)以及 # Trim()(去掉串左右兩邊的空格)。
mysql> select Concat(vend_name, ' (', vend_country, ')') as vend_title from vendors order by vend_name; +-------------------------+ | vend_title | +-------------------------+ | ACME (USA) | | Anvils R Us (USA) | | Furball Inc. (USA) | | Jet Set (England) | | Jouets Et Ours (France) | | LT Supplies (USA) | +-------------------------+ 6 rows in set (0.00 sec)
mysql> select prod_id, quantity, item_price, quantity*item_price as expanded_price from orderitems where order_num='20005'; +---------+----------+------------+----------------+ | prod_id | quantity | item_price | expanded_price | +---------+----------+------------+----------------+ | ANV01 | 10 | 5.99 | 59.90 | | ANV02 | 3 | 9.99 | 29.97 | | TNT2 | 5 | 10.00 | 50.00 | | FB | 1 | 10.00 | 10.00 | +---------+----------+------------+----------------+ 4 rows in set (0.00 sec)
mysql> select prod_name, Upper(prod_name), prod_price from products where prod_name regexp '.000'; +--------------+------------------+------------+ | prod_name | Upper(prod_name) | prod_price | +--------------+------------------+------------+ | JetPack 1000 | JETPACK 1000 | 35.00 | | JetPack 2000 | JETPACK 2000 | 55.00 | +--------------+------------------+------------+ 2 rows in set (0.00 sec)
mysql> select order_date, cust_id from orders where Date(order_date) = '2005-09-01'; +---------------------+---------+ | order_date | cust_id | +---------------------+---------+ | 2005-09-01 00:00:00 | 10001 | +---------------------+---------+ 1 row in set (0.00 sec)
mysql> select order_date, cust_id from orders where Year(order_date) = '2005' and Month(order_date) = '09'; +---------------------+---------+ | order_date | cust_id | +---------------------+---------+ | 2005-09-01 00:00:00 | 10001 | | 2005-09-12 00:00:00 | 10003 | | 2005-09-30 00:00:00 | 10004 | +---------------------+---------+ 3 rows in set (0.00 sec)
mysql> select AVG(prod_price) as avg_price from products; +-----------+ | avg_price | +-----------+ | 16.133571 | +-----------+ 1 row in set (0.01 sec)
mysql> select AVG(prod_price) as avg_price from products where vend_id = '1003'; +-----------+ | avg_price | +-----------+ | 13.212857 | +-----------+ 1 row in set (0.01 sec)
mysql> select COUNT(*) as num_cust from customers; +----------+ | num_cust | +----------+ | 5 | +----------+ 1 row in set (0.00 sec)
mysql> select COUNT(cust_email) as num_cust from customers; +----------+ | num_cust | +----------+ | 3 | +----------+ 1 row in set (0.00 sec)
mysql> select MAX(prod_price) as max_price from products; +-----------+ | max_price | +-----------+ | 55.00 | +-----------+ 1 row in set (0.00 sec)
mysql> select MIN(prod_price) as max_price from products; +-----------+ | max_price | +-----------+ | 2.50 | +-----------+ 1 row in set (0.00 sec)
mysql> select SUM(quantity) as ieems_orders from orderitems where order_num = '20005'; +--------------+ | ieems_orders | +--------------+ | 19 | +--------------+ 1 row in set (0.00 sec)
以上5個彙集函數均可以以下使用:
對全部的行執行計算,指定ALL參數或不給參數(由於ALL是默認
行爲);
只包含不一樣的值,指定DISTINCT參數。程序員
mysql> select AVG(distinct prod_price) as avg_price from products where vend_id = '1003'; +-----------+ | avg_price | +-----------+ | 15.998000 | +-----------+ 1 row in set (0.00 sec)
mysql> select COUNT(*) as num_items, MIN(prod_price) as min_price, MAX(prod_price) as max_price, AVG(prod_price) as avg_price from products; +-----------+-----------+-----------+-----------+ | num_items | min_price | max_price | avg_price | +-----------+-----------+-----------+-----------+ | 14 | 2.50 | 55.00 | 16.133571 | +-----------+-----------+-----------+-----------+ 1 row in set (0.00 sec)
mysql> select vend_id, COUNT(*) as num_prods from products group by vend_id; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1001 | 3 | | 1002 | 2 | | 1003 | 7 | | 1005 | 2 | +---------+-----------+ 4 rows in set (0.00 sec)
mysql> select vend_id, COUNT(*) as num_prods from products group by vend_id with rollup; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1001 | 3 | | 1002 | 2 | | 1003 | 7 | | 1005 | 2 | | NULL | 14 | +---------+-----------+ 5 rows in set (0.00 sec)
在具體使用GROUP BY子句前,須要知道一些重要的規定。正則表達式
GROUP BY子句能夠包含任意數目的列。這使得能對分組進行嵌套,sql
爲數據分組提供更細緻的控制。數據庫
若是在GROUP BY子句中嵌套了分組,數據將在最後規定的分組上安全
進行彙總。換句話說,在創建分組時,指定的全部列都一塊兒計算app
(因此不能從個別的列取回數據)。less
GROUP BY子句中列出的每一個列都必須是檢索列或有效的表達式ide
(但不能是彙集函數)。若是在SELECT中使用表達式,則必須在函數
GROUP BY子句中指定相同的表達式。不能使用別名。
除彙集計算語句外, SELECT語句中的每一個列都必須在GROUP BY子
句中給出。
若是分組列中具備NULL值,則NULL將做爲一個分組返回。若是列
中有多行NULL值,它們將分爲一組 。
GROUP BY子句必須出如今WHERE子句以後, ORDER BY子句以前 。
mysql> select vend_id, COUNT(*) as num_prods from products group by vend_id having COUNT(*) >=3; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1001 | 3 | | 1003 | 7 | +---------+-----------+ 2 rows in set (0.00 sec)
mysql> select vend_id, COUNT(*) as num_prods from products where prod_price >= 10 group by vend_id having COUNT(*) >=2; +---------+-----------+ | vend_id | num_prods | +---------+-----------+ | 1003 | 4 | | 1005 | 2 | +---------+-----------+ 2 rows in set (0.00 sec)
mysql> select order_num, SUM(quantity*item_price) as ordertotal -> from orderitems -> group by order_num -> having SUM(quantity*item_price) >= 50; +-----------+------------+ | order_num | ordertotal | +-----------+------------+ | 20005 | 149.87 | | 20006 | 55.00 | | 20007 | 1000.00 | | 20008 | 125.00 | +-----------+------------+ 4 rows in set (0.00 sec)
mysql> select order_num, SUM(quantity*item_price) as ordertotal -> from orderitems -> group by order_num -> having SUM(quantity*item_price) >= 50 -> order by ordertotal; +-----------+------------+ | order_num | ordertotal | +-----------+------------+ | 20006 | 55.00 | | 20008 | 125.00 | | 20005 | 149.87 | | 20007 | 1000.00 | +-----------+------------+ 4 rows in set (0.00 sec)
mysql> select cust_id from orders where order_num in (select order_num from orderitems where prod_id = 'TNT2'); +---------+ | cust_id | +---------+ | 10001 | | 10004 | +---------+ 2 rows in set (0.00 sec)
mysql> select cust_id, cust_contact -> from customers -> where cust_id in ( -> select cust_id from orders where order_num in ( -> select order_num from orderitems where prod_id = 'TNT2')); +---------+--------------+ | cust_id | cust_contact | +---------+--------------+ | 10001 | Y Lee | | 10004 | Y Sam | +---------+--------------+ 2 rows in set (0.00 sec)
(1) 從customers表中檢索客戶列表。
(2) 對於檢索出的每一個客戶,統計其在orders表中的訂單數目。
mysql> select cust_name, -> cust_state, -> (select COUNT(*) -> from orders -> where orders.cust_id = customers.cust_id) as orders -> from customers -> order by cust_name; +----------------+------------+--------+ | cust_name | cust_state | orders | +----------------+------------+--------+ | Coyote Inc. | MI | 2 | | E Fudd | IL | 1 | | Mouse House | OH | 0 | | Wascals | IN | 1 | | Yosemite Place | AZ | 1 | +----------------+------------+--------+ 5 rows in set (0.00 sec)
mysql> select vend_name, prod_id, prod_price -> from vendors, products -> where vendors.vend_id = products.vend_id -> order by vend_name, prod_name; +-------------+---------+------------+ | vend_name | prod_id | prod_price | +-------------+---------+------------+ | ACME | FB | 10.00 | | ACME | FC | 2.50 | | ACME | DTNTR | 13.00 | | ACME | SAFE | 50.00 | | ACME | SLING | 4.49 | | ACME | TNT1 | 2.50 | | ACME | TNT2 | 10.00 | | Anvils R Us | ANV01 | 5.99 | | Anvils R Us | ANV02 | 9.99 | | Anvils R Us | ANV03 | 14.99 | | Jet Set | JP1000 | 35.00 | | Jet Set | JP2000 | 55.00 | | LT Supplies | FU1 | 3.42 | | LT Supplies | OL1 | 8.99 | +-------------+---------+------------+ 14 rows in set (0.00 sec)
mysql> select vend_name, prod_id, prod_price -> from vendors inner join products -> on vendors.vend_id = products.vend_id -> order by vend_name, prod_name; +-------------+---------+------------+ | vend_name | prod_id | prod_price | +-------------+---------+------------+ | ACME | FB | 10.00 | | ACME | FC | 2.50 | | ACME | DTNTR | 13.00 | | ACME | SAFE | 50.00 | | ACME | SLING | 4.49 | | ACME | TNT1 | 2.50 | | ACME | TNT2 | 10.00 | | Anvils R Us | ANV01 | 5.99 | | Anvils R Us | ANV02 | 9.99 | | Anvils R Us | ANV03 | 14.99 | | Jet Set | JP1000 | 35.00 | | Jet Set | JP2000 | 55.00 | | LT Supplies | FU1 | 3.42 | | LT Supplies | OL1 | 8.99 | +-------------+---------+------------+ 14 rows in set (0.00 sec)
mysql> select cust_name, cust_contact -> from customers, orders, orderitems -> where customers.cust_id = orders.cust_id -> and orders.order_num = orderitems.order_num -> and prod_id = 'TNT2'; +----------------+--------------+ | cust_name | cust_contact | +----------------+--------------+ | Coyote Inc. | Y Lee | | Yosemite Place | Y Sam | +----------------+--------------+ 2 rows in set (0.00 sec)
# 使用列別名 mysql> select Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') as -> vend_title -> from vendors -> order by vend_name; +-------------------------+ | vend_title | +-------------------------+ | ACME (USA) | | Anvils R Us (USA) | | Furball Inc. (USA) | | Jet Set (England) | | Jouets Et Ours (France) | | LT Supplies (USA) | +-------------------------+ 6 rows in set (0.00 sec)
# 使用表別名 mysql> select cust_name, cust_contact -> from customers as c, orders as o, orderitems as oi -> where c.cust_id = o.cust_id -> and oi.order_num = o.order_num -> and prod_id = 'TNT2'; +----------------+--------------+ | cust_name | cust_contact | +----------------+--------------+ | Coyote Inc. | Y Lee | | Yosemite Place | Y Sam | +----------------+--------------+ 2 rows in set (0.00 sec)
假如你發現某物品(其ID爲DTNTR)存在問題,所以想知道生產該物
品的供應商生產的其餘物品是否也存在這些問題。此查詢要求首先找到
生產ID爲DTNTR的物品的供應商,而後找出這個供應商生產的其餘物品。
# 使用子查詢 mysql> select prod_id, prod_name -> from products -> where vend_id = (select vend_id -> from products -> where prod_id = 'DTNTR'); +---------+----------------+ | prod_id | prod_name | +---------+----------------+ | DTNTR | Detonator | | FB | Bird seed | | FC | Carrots | | SAFE | Safe | | SLING | Sling | | TNT1 | TNT (1 stick) | | TNT2 | TNT (5 sticks) | +---------+----------------+ 7 rows in set (0.00 sec)
# 使用自聯結 mysql> select p1.prod_id, p1.prod_name -> from products as p1, products as p2 -> where p1.vend_id = p2.vend_id -> and p2.prod_id = 'DTNTR'; +---------+----------------+ | prod_id | prod_name | +---------+----------------+ | DTNTR | Detonator | | FB | Bird seed | | FC | Carrots | | SAFE | Safe | | SLING | Sling | | TNT1 | TNT (1 stick) | | TNT2 | TNT (5 sticks) | +---------+----------------+ 7 rows in set (0.00 sec)
mysql> select c.*, o.order_num, o.order_date, oi.prod_id, oi.quantity, oi.item_price -> from customers as c, orders as o, orderitems as oi -> where c.cust_id = o.cust_id -> and oi.order_num = o.order_num -> and oi.prod_id = 'FB'; +---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | order_num | order_date | prod_id | quantity | item_price | +---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+ | 10001 | Coyote Inc. | 200 Maple Lane | Detroit | MI | 44444 | USA | Y Lee | ylee@coyote.com | 20005 | 2005-09-01 00:00:00 | FB | 1 | 10.00 | | 10001 | Coyote Inc. | 200 Maple Lane | Detroit | MI | 44444 | USA | Y Lee | ylee@coyote.com | 20009 | 2005-10-08 00:00:00 | FB | 1 | 10.00 | +---------+-------------+----------------+-----------+------------+----------+--------------+--------------+-----------------+-----------+---------------------+---------+----------+------------+ 2 rows in set (0.00 sec)
# 內鏈接 # 檢索全部客戶及其訂單 mysql> select customers.cust_id, orders.order_num -> from customers inner join orders -> on customers.cust_id = orders.cust_id; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10001 | 20009 | | 10003 | 20006 | | 10004 | 20007 | | 10005 | 20008 | +---------+-----------+ 5 rows in set (0.00 sec)
# 外部聯結 # 包含那些沒有訂單的客戶 mysql> select customers.cust_id, orders.order_num -> from customers left outer join orders -> on customers.cust_id = orders.cust_id; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10001 | 20009 | | 10002 | NULL | | 10003 | 20006 | | 10004 | 20007 | | 10005 | 20008 | +---------+-----------+ 6 rows in set (0.00 sec)
# 檢索全部客戶及每一個客戶所下的訂單數 mysql> select customers.cust_name, customers.cust_id, COUNT(orders.order_num) as num_ord -> from customers inner join orders -> on customers.cust_id = orders.cust_id -> group by customers.cust_id; +----------------+---------+---------+ | cust_name | cust_id | num_ord | +----------------+---------+---------+ | Coyote Inc. | 10001 | 2 | | Wascals | 10003 | 1 | | Yosemite Place | 10004 | 1 | | E Fudd | 10005 | 1 | +----------------+---------+---------+ 4 rows in set (0.00 sec)
# 包含沒有任何訂單的客戶 mysql> select customers.cust_name, customers.cust_id, COUNT(orders.order_num) as num_ord -> from customers left outer join orders -> on customers.cust_id = orders.cust_id -> group by customers.cust_id; +----------------+---------+---------+ | cust_name | cust_id | num_ord | +----------------+---------+---------+ | Coyote Inc. | 10001 | 2 | | Mouse House | 10002 | 0 | | Wascals | 10003 | 1 | | Yosemite Place | 10004 | 1 | | E Fudd | 10005 | 1 | +----------------+---------+---------+ 5 rows in set (0.00 sec)
在總結關於聯結的這兩章前,有必要彙總一下關於聯結及其使用的
某些要點。
注意所使用的聯結類型。通常咱們使用內部聯結,但使用外部聯
結也是有效的。
保證使用正確的聯結條件,不然將返回不正確的數據。
應該老是提供聯結條件,不然會得出笛卡兒積。
在一個聯結中能夠包含多個表,甚至對於每一個聯結能夠採用不一樣
的聯結類型。雖然這樣作是合法的,通常也頗有用,但應該在一
起測試它們前,分別測試每一個聯結。這將使故障排除更爲簡單。
# where查詢 mysql> select vend_id, prod_id, prod_price -> from products -> where prod_price <= 5 or vend_id in (1001, 1002); +---------+---------+------------+ | vend_id | prod_id | prod_price | +---------+---------+------------+ | 1001 | ANV01 | 5.99 | | 1001 | ANV02 | 9.99 | | 1001 | ANV03 | 14.99 | | 1003 | FC | 2.50 | | 1002 | FU1 | 3.42 | | 1002 | OL1 | 8.99 | | 1003 | SLING | 4.49 | | 1003 | TNT1 | 2.50 | +---------+---------+------------+ 8 rows in set (0.00 sec)
# union mysql> select vend_id, prod_id, prod_price -> from products -> where prod_price <= 5 -> union -> select vend_id, prod_id, prod_price -> from products -> where vend_id in (1001, 1002); +---------+---------+------------+ | vend_id | prod_id | prod_price | +---------+---------+------------+ | 1003 | FC | 2.50 | | 1002 | FU1 | 3.42 | | 1003 | SLING | 4.49 | | 1003 | TNT1 | 2.50 | | 1001 | ANV01 | 5.99 | | 1001 | ANV02 | 9.99 | | 1001 | ANV03 | 14.99 | | 1002 | OL1 | 8.99 | +---------+---------+------------+ 8 rows in set (0.00 sec)
UNION與WHERE 本章開始時說過,UNION幾乎老是完成與多個
WHERE條件相同的工做。UNION ALL爲UNION的一種形式,它完成
WHERE子句完成不了的工做。若是確實須要每一個條件的匹配行全
部出現(包括重複行),則必須使用UNION ALL而不是WHERE。
# 排序union查詢 mysql> select vend_id, prod_id, prod_price -> from products -> where prod_price <= 5 -> union -> select vend_id, prod_id, prod_price -> from products -> where vend_id in (1001, 1002) -> order by vend_id, prod_price; +---------+---------+------------+ | vend_id | prod_id | prod_price | +---------+---------+------------+ | 1001 | ANV01 | 5.99 | | 1001 | ANV02 | 9.99 | | 1001 | ANV03 | 14.99 | | 1002 | FU1 | 3.42 | | 1002 | OL1 | 8.99 | | 1003 | FC | 2.50 | | 1003 | TNT1 | 2.50 | | 1003 | SLING | 4.49 | +---------+---------+------------+ 8 rows in set (0.00 sec)
mysql> select note_text -> from productnotes -> where Match(note_text) Against('rabbit'); +-----------------------------------------------------------------------------------------------------------------------+ | note_text | +-----------------------------------------------------------------------------------------------------------------------+ | Customer complaint: rabbit has been able to detect trap, food apparently less effective now. | | Quantity varies, sold by the sack load. All guaranteed to be bright and orange, and suitable for use as rabbit bait. | +-----------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
mysql> select note_text, Match(note_text) Against('rabbit') as rank -> from productnotes;
# 未使用查詢擴展 mysql> select note_text -> from productnotes -> where Match(note_text) Against('anvils'); +----------------------------------------------------------------------------------------------------------------------------------------------------------+ | note_text | +----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils. | +----------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
# 使用擴展 mysql> select note_text -> from productnotes -> where Match(note_text) Against('anvils' with query expansion); +----------------------------------------------------------------------------------------------------------------------------------------------------------+ | note_text | +----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils. | | Customer complaint: Sticks not individually wrapped, too easy to mistakenly detonate all at once. Recommend individual wrapping. | | Customer complaint: Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead. | | Please note that no returns will be accepted if safe opened using explosives. | | Customer complaint: rabbit has been able to detect trap, food apparently less effective now. | | Customer complaint: Circular hole in safe floor can apparently be easily cut with handsaw. | | Matches not included, recommend purchase of matches or detonator (item DTNTR). | +----------------------------------------------------------------------------------------------------------------------------------------------------------+ 7 rows in set (0.00 sec)
此次返回了7行。第一行包含詞anvils,所以等級最高。第二
行與anvils無關,但由於它包含第一行中的兩個詞(customer
和recommend),因此也被檢索出來。第3行也包含這兩個相同的詞,但它
們在文本中的位置更靠後且分開得更遠,所以也包含這一行,但等級爲
第三。第三行確實也沒有涉及anvils(按它們的產品名)。
mysql> insert into customers values(null, 'Prp E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA',null, null); Query OK, 1 row affected (0.06 sec)
mysql> insert into customers(cust_name, -> cust_address, -> cust_city, -> cust_state, -> cust_zip, -> cust_country, -> cust_contact, -> cust_email) -> values('Pep E. LaPew', -> '100 main street', -> 'Los Angeles', -> 'CA', -> '90046', -> 'USA', -> null, -> null); Query OK, 1 row affected (0.04 sec)
其中單條INSERT語句有多組值,每組值用一對圓括號括起來,
用逗號分隔。
insert select
# id爲10005的顧客增長Email mysql> update customers -> set cust_email = 'emler@fudd.com' -> where cust_id = '10005'; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from customers -> where cust_id = '10006'; Query OK, 1 row affected (0.04 sec)
更快的刪除 若是想從表中刪除全部行,不要使用DELETE。
可以使用TRUNCATE TABLE語句,它完成相同的工做,但速度更
快(TRUNCATE實際是刪除原來的表並從新建立一個表,而不
是逐行刪除表中的數據)。
下面是許多SQL程序員使用UPDATE或DELETE時所遵循的習慣。
除非確實打算更新和刪除每一行,不然絕對不要使用不帶WHERE
子句的UPDATE或DELETE語句。
保證每一個表都有主鍵(若是忘記這個內容,請參閱第15章),儘量
像WHERE子句那樣使用它(能夠指定各主鍵、多個值或值的範圍)。
在對UPDATE或DELETE語句使用WHERE子句前,應該先用SELECT進
行測試,保證它過濾的是正確的記錄,以防編寫的WHERE子句不
正確。
使用強制實施引用完整性的數據庫(關於這個內容,請參閱第15
章),這樣MySQL將不容許刪除具備與其餘表相關聯的數據的行。
mysql> create view productcustomers as -> select cust_name, cust_contact, prod_id -> from customers, orders, orderitems -> where customers.cust_id = orders.cust_id -> and orderitems.order_num = orders.order_num; Query OK, 0 rows affected (0.03 sec) mysql> select * from productcustomers; +----------------+--------------+---------+ | cust_name | cust_contact | prod_id | +----------------+--------------+---------+ | Coyote Inc. | Y Lee | ANV01 | | Coyote Inc. | Y Lee | ANV02 | | Coyote Inc. | Y Lee | TNT2 | | Coyote Inc. | Y Lee | FB | | Coyote Inc. | Y Lee | FB | | Coyote Inc. | Y Lee | OL1 | | Coyote Inc. | Y Lee | SLING | | Coyote Inc. | Y Lee | ANV03 | | Wascals | Jim Jones | JP2000 | | Yosemite Place | Y Sam | TNT2 | | E Fudd | E Fudd | FC | +----------------+--------------+---------+ 11 rows in set (0.00 sec)
mysql> create view vendorlocations as -> select Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') as vend_title -> from vendors -> order by vend_name; Query OK, 0 rows affected (0.03 sec) mysql> select * from vendorlocations; +-------------------------+ | vend_title | +-------------------------+ | ACME (USA) | | Anvils R Us (USA) | | Furball Inc. (USA) | | Jet Set (England) | | Jouets Et Ours (France) | | LT Supplies (USA) | +-------------------------+ 6 rows in set (0.00 sec)
mysql> create view customeremaillist as -> select cust_id, cust_name, cust_email -> from customers -> where cust_email is not null; Query OK, 0 rows affected (0.03 sec) mysql> select * from customeremaillist; +---------+----------------+---------------------+ | cust_id | cust_name | cust_email | +---------+----------------+---------------------+ | 10001 | Coyote Inc. | ylee@coyote.com | | 10003 | Wascals | rabbit@wascally.com | | 10004 | Yosemite Place | sam@yosemite.com | | 10005 | E Fudd | emler@fudd.com | +---------+----------------+---------------------+ 4 rows in set (0.00 sec)
mysql> create view orderitemsexpanded as -> select order_num, prod_id, quantity, item_price, quantity*item_price as expanded_price -> from orderitems; Query OK, 0 rows affected (0.02 sec) mysql> select * from orderitemsexpanded -> where order_num = '20005'; +-----------+---------+----------+------------+----------------+ | order_num | prod_id | quantity | item_price | expanded_price | +-----------+---------+----------+------------+----------------+ | 20005 | ANV01 | 10 | 5.99 | 59.90 | | 20005 | ANV02 | 3 | 9.99 | 29.97 | | 20005 | TNT2 | 5 | 10.00 | 50.00 | | 20005 | FB | 1 | 10.00 | 10.00 | +-----------+---------+----------+------------+----------------+ 4 rows in set (0.00 sec)
# 在客戶端執行切換分隔符 mysql> delimiter // mysql> create procedure productpricing( -> out pl decimal(8,2), -> out ph decimal(8,2), -> out pa decimal(8,2)) -> begin -> select Min(prod_price) -> into pl -> from products; -> select Max(prod_price) -> into ph -> from products; -> select Avg(prod_price) -> into pa -> from products; -> end// Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> call productpricing(@pricelow, @pricehigh, @priceavg); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select @pricelow, @pricehigh, @priceavg; +-----------+------------+-----------+ | @pricelow | @pricehigh | @priceavg | +-----------+------------+-----------+ | 2.50 | 55.00 | 16.13 | +-----------+------------+-----------+ 1 row in set (0.00 sec)
mysql> create trigger newproduct after insert on products -> for each row select 'Product added' into @ee; Query OK, 0 rows affected (0.06 sec) mysql> select @ee; +------+ | @ee | +------+ | NULL | +------+ 1 row in set (0.00 sec) mysql> desc products; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | prod_id | char(10) | NO | PRI | NULL | | | vend_id | int(11) | NO | MUL | NULL | | | prod_name | char(255) | NO | | NULL | | | prod_price | decimal(8,2) | NO | | NULL | | | prod_desc | text | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 5 rows in set (0.01 sec) mysql> insert products(prod_id, vend_id, prod_name, prod_price) -> values('20009', '1002', 'ysxu', 2.56); Query OK, 1 row affected (0.04 sec) mysql> select @ee; +---------------+ | @ee | +---------------+ | Product added | +---------------+ 1 row in set (0.00 sec)
create user [用戶名] identified by [用戶密碼]
rename user [舊用戶名] to [新用戶名]
drop user [用戶名]
# 查看用戶的權限 show grants for [用戶名] //完整用戶名:'用戶名'@'主機' # 設置訪問權限 grant all on *.* to [用戶名] all-全部權限 *.*-全部數據庫下全部表 #grant的反操做是revoke
set password for [用戶名] = Password('密碼') # 設置當前用戶的密碼 set password = Passwprd('密碼')