下面以一個例子說明如何使用SELECT從單個表中獲取數據。
首先定義數據表,輸入語句以下:
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)
);
爲了演示如何使用SELECT語句,須要插入以下數據:
INSERT INTO fruits (f_id, s_id, f_name, f_price)
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.6),
('m2',105,'xbabay', 2.6),
('t4',107,'xbababa', 3.6),
('m3',105,'xxtt', 11.6),
('b5',107,'xxxx', 3.6);
使用SELECT語句查詢f_id字段的數據。
SELECT f_id, f_name FROM fruits;
【例7.1】從fruits表中檢索全部字段的數據,SQL語句以下:
SELECT * FROM fruits;正則表達式
【例7.2】查詢fruits表中f_name列全部水果名稱,SQL語句以下:
SELECT f_name FROM fruits;
該語句使用SELECT聲明從fruits表中獲取名稱爲f_name字段下的全部水果名稱,指定字段的名稱緊跟在SELECT關鍵字以後,查詢結果以下:
SELECT f_name FROM fruits;數據庫
【例7.3】例如,從fruits表中獲取f_name和f_price兩列,SQL語句以下:
SELECT f_name, f_price FROM fruits;
該語句使用SELECT聲明從fruits表中獲取名稱爲f_name和f_price兩個字段下的全部水果名稱和價格,兩個字段之間用逗號分隔開,查詢結果以下:
SELECT f_name, f_price FROM fruits;app
【例7.4】查詢價格爲10.2元的水果的名稱,SQL語句以下:
SELECT f_name, f_price
FROM fruits
WHERE f_price = 10.2;ide
【例7.5】查找名稱爲「apple」的水果的價格,SQL語句以下:
SELECT f_name, f_price
FROM fruits
WHERE f_name = 'apple';
【例7.6】查詢價格小於10的水果的名稱,SQL語句以下:
SELECT f_name, f_price
FROM fruits
WHERE f_price < 10;函數
【例7.7】s_id爲101和102的記錄,SQL語句以下:
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id IN (101,102)
ORDER BY f_name;ui
【例7.8】查詢全部s_id不等於101也不等於102的記錄,SQL語句以下:
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id NOT IN (101,102)
ORDER BY f_name;排序
【例7.9】查詢價格在2.00元到10.20元之間的水果名稱和價格,SQL語句以下:
SELECT f_name, f_price FROM fruits WHERE f_price BETWEEN 2.00 AND 10.20;
查詢結果以下:
SELECT f_name, f_price
FROM fruits
WHERE f_price BETWEEN 2.00 AND 10.20;
【例7.10】查詢價格在2.00元到10.20元以外的水果名稱和價格,SQL語句以下:
SELECT f_name, f_price
FROM fruits
WHERE f_price NOT BETWEEN 2.00 AND 10.20;ip
【例7.11】查找全部以’b’字母開頭的水果,SQL語句以下:
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE 'b%';
【例7.12】在fruits表中,查詢f_name中包含字母’g’的記錄,SQL語句以下:
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE '%g%';
【例7.13】查詢以’b’開頭,並以’y’結尾的水果的名稱,SQL語句以下:
SELECT f_name
FROM fruits
WHERE f_name LIKE 'b%y';ci
【例7.14】在fruits表中,查詢以字母’y’結尾,且’y’前面只有4個字母的記錄,SQL語句以下:
SELECT f_id, f_name FROM fruits WHERE f_name LIKE '----y';字符串
下面,在數據庫中建立數據表customers,該表中包含了本章中須要用到的數據。
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(10) NULL,
c_contact char(50) NULL,
c_email char(255) NULL,
PRIMARY KEY (c_id)
);
爲了演示須要插入數據,請讀者插入執行如下語句。
INSERT INTO customers(c_id, c_name, c_address, c_city,
c_zip, c_contact, c_email)
VALUES(10001, 'RedHook', '200 Street ', 'Tianjin',
'300000', 'LiMing', 'LMing@163.com'),
(10002, 'Stars', '333 Fromage Lane',
'Dalian', '116000', 'Zhangbo','Jerry@hotmail.com'),
(10003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000',
'LuoCong', NULL),
(10004, 'JOTO', '829 Riverside Drive', 'Haikou',
'570000', 'YangShan', 'sam@hotmail.com');
SELECT COUNT(*) AS cust_num FROM customers;
【例7.15】查詢customers表中c_email爲空的記錄的c_id、c_name和c_email字段值,SQL語句以下:
SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NULL;
查詢結果以下:
SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NULL;
【例7.16】查詢customers表中c_email不爲空的記錄的c_id、c_name和c_email字段值,SQL語句以下:
SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NOT NULL;
【例7.17】在fruits表中查詢s_id = 101,而且f_price大於等於5的水果價格和名稱,SQL語句以下:
SELECT f_id, f_price, f_name FROM fruits WHERE s_id = '101' AND f_price >=5;
【例7.18】在fruits表中查詢s_id = 101或者102,且f_price大於5,而且f_name=‘apple’的水果價格和名稱,SQL語句以下:
SELECT f_id, f_price, f_name FROM fruits
WHERE s_id IN('101', '102') AND f_price >= 5 AND f_name = 'apple';
【例7.19】查詢s_id=101或者s_id=102的水果供應商的f_price和f_name,SQL語句以下:
SELECT s_id,f_name, f_price FROM fruits WHERE s_id = 101 OR s_id = 102;
查詢結果以下:
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id = 101 OR s_id = 102;
【例7.20】查詢s_id=101或者s_id=102的水果供應商的f_price和f_name,SQL語句以下:
SELECT s_id,f_name, f_price FROM fruits WHERE s_id IN(101,102);
查詢結果以下:
SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id IN(101,102);
【例7.21】查詢fruits表中s_id字段的值,返回s_id字段值且不得重複,SQL語句以下:
SELECT DISTINCT s_id FROM fruits;
【例7.22】查詢fruits表的f_name字段值,並對其進行排序,SQL語句以下:
SELECT f_name FROM fruits ORDER BY f_name;
【例7.23】查詢fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序,SQL語句以下:
SELECT f_name, f_price FROM fruits ORDER BY f_name, f_price;
【例7.24】查詢fruits表中的f_name和f_price字段,對結果按f_price降序方式排序,SQL語句以下:
SELECT f_name, f_price FROM fruits ORDER BY f_price DESC;
【例7.25】查詢fruits表,先按f_price降序排序,再按f_name字段升序排序,SQL語句以下:
SELECT f_price, f_name FROM fruits ORDER BY f_price DESC, f_name;
【例7.26】根據s_id對fruits表中的數據進行分組,SQL語句以下:
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;
【例7.27】根據s_id對fruits表中的數據進行分組,將每一個供應商的水果名稱顯示出來,SQL語句以下:
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id;
【例7.28】根據s_id對fruits表中的數據進行分組,並顯示水果種類大於1的分組信息,SQL語句以下:
SELECT s_id, GROUP_CONCAT(f_name) AS Names
FROM fruits
GROUP BY s_id HAVING COUNT(f_name) > 1;
【例7.29】根據s_id對fruits表中的數據進行分組,並顯示記錄數量,SQL語句以下:
SELECT s_id, COUNT(*) AS Total
FROM fruits
GROUP BY s_id WITH ROLLUP;
【例7.30】根據s_id和f_name字段對fruits表中的數據進行分組, SQL語句以下,
SELECT * FROM fruits group by s_id,f_name;
爲了演示效果,首先建立數據表,SQL語句以下:
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)
) ;
而後插入演示數據。SQL語句以下:
INSERT INTO orderitems(o_num, o_item, f_id, quantity, item_price)
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);
【例7.31】查詢訂單價格大於100的訂單號和總訂單價格,SQL語句以下:
SELECT o_num, SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100;
能夠看到,返回的結果中orderTotal列的總訂單價格並無按照必定順序顯示,接下來,使用ORDER BY關鍵字按總訂單價格排序顯示結果,SQL語句以下:
SELECT o_num, SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100
ORDER BY orderTotal;
【例7.32】顯示fruits表查詢結果的前4行,SQL語句以下:
SELECT * From fruits LIMIT 4;
【例7.33】在fruits表中,使用LIMIT子句,返回從第5個記錄開始的,行數長度爲3的記錄,SQL語句以下:
SELECT * From fruits LIMIT 4, 3;
【例7.34】查詢customers表中總的行數,SQL語句以下:
SELECT COUNT(*) AS cust_num
FROM customers;
【例7.35】查詢customers表中有電子郵箱的顧客的總數,SQL語句以下:
SELECT COUNT(c_email) AS email_num
FROM customers;
【例7.36】在orderitems表中,使用COUNT()函數統計不一樣訂單號中訂購的水果種類,SQL語句以下:
SELECT o_num, COUNT(f_id)
FROM orderitems
GROUP BY o_num;
【例7.37】在orderitems表中查詢30005號訂單一共購買的水果總量,SQL語句以下:
SELECT SUM(quantity) AS items_total
FROM orderitems
WHERE o_num = 30005;
【例7.38】在orderitems表中,使用SUM()函數統計不一樣訂單號中訂購的水果總量,SQL語句以下:
SELECT o_num, SUM(quantity) AS items_total
FROM orderitems
GROUP BY o_num;
【例7.39】在fruits表中,查詢s_id=103的供應商的水果價格的平均值,SQL語句以下:
SELECT AVG(f_price) AS avg_price
FROM fruits
WHERE s_id = 103;
【例7.40】在fruits表中,查詢每個供應商的水果價格的平均值,SQL語句以下:
SELECT s_id,AVG(f_price) AS avg_price
FROM fruits
GROUP BY s_id;
【例7.41】在fruits表中查找市場上價格最高的水果,SQL語句以下:
SELECT MAX(f_price) AS max_price FROM fruits;
【例7.42】在fruits表中查找不一樣供應商提供的價格最高的水果,SQL語句以下:
SELECT s_id, MAX(f_price) AS max_price
FROM fruits
GROUP BY s_id;
【例7.43】在fruits表中查找f_name的最大值,SQL語句以下:
SELECT MAX(f_name) FROM fruits;
【例7.44】在fruits表中查找市場上價格最低的水果,SQL語句以下:
SELECT MIN(f_price) AS min_price FROM fruits;
【例7.45】在fruits表中查找不一樣供應商提供的價格最低的水果,SQL語句以下:
SELECT s_id, MIN(f_price) AS min_price
FROM fruits
GROUP BY s_id;
爲了演示的須要,首先建立數據表suppliers,SQL語句以下:
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)
) ;
插入須要演示的數據,SQL語句以下:
INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call)
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','Taiyuang','030000', '22222'),
(106,'Just Eat Ours','Beijing','010', '45678'),
(107,'DK Inc.','Zhengzhou','450000', '33332');
【例7.46】在fruits表和suppliers表之間使用內鏈接查詢。
查詢以前,查看兩個表的結構:
DESC fruits;
DESC suppliers;
由結果能夠看到,fruits表和suppliers表中都有相同數據類型的字段s_id,兩個表經過s_id字段創建聯繫。接下來從fruits表中查詢f_name、f_price字段,從suppliers表中查詢s_id、s_name,SQL語句以下:
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;
【例7.47】在fruits表和suppliers表之間,使用INNER JOIN語法進行內鏈接查詢,SQL語句以下:
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id;
【例7.48】查詢供應f_id= ‘a1’的水果供應商提供的其餘水果種類,SQL語句以下:
SELECT f1.f_id, f1.f_name
FROM fruits AS f1, fruits AS f2
WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';
1.LEFT JOIN左鏈接
左鏈接的結果包括LEFT OUTER子句中指定的左表的全部行,而不只僅是鏈接列所匹配的行。若是左表的某行在右表中沒有匹配行,則在相關聯的結果行中,右表的全部選擇列表列均爲空值。
首先建立表orders,SQL語句以下:
CREATE TABLE orders
(
o_num int NOT NULL AUTO_INCREMENT,
o_date datetime NOT NULL,
c_id int NOT NULL,
PRIMARY KEY (o_num)
) ;
插入須要演示的數據,SQL語句以下:
INSERT INTO orders(o_num, o_date, c_id)
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);
【例7.49】在customers表和orders表中,查詢全部客戶,包括沒有訂單的客戶,SQL語句以下:
SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;
2.RIGHT JOIN右鏈接
右鏈接是左鏈接的反向鏈接,將返回右表的全部行。若是右表的某行在左表中沒有匹配行,左表將返回空值。
【例7.50】在customers表和orders表中,查詢全部訂單,包括沒有客戶的訂單,SQL語句以下:
SELECT customers.c_id, orders.o_num
FROM customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;
【例7.51】在customers表和orders表中,使用INNER JOIN語法查詢customers表中ID爲10001的客戶的訂單信息,SQL語句以下:
SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;
【例7.52】在fruits表和suppliers表之間,使用INNER JOIN語法進行內鏈接查詢,並對查詢結果排序,SQL語句以下:
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;
7.5.1 帶ANY、SOME關鍵字的子查詢
ANY和SOME關鍵字是同義詞,表示知足其中任一條件,它們容許建立一個表達式對子查詢的返回值列表進行比較,只要知足內層子查詢中的任何一個比較條件,就返回一個結果做爲外層查詢的條件。
下面定義兩個表tb1和tb2:
CREATE table tbl1 ( num1 INT NOT NULL);
CREATE table tbl2 ( num2 INT NOT NULL);
分別向兩個表中插入數據:
INSERT INTO tbl1 values(1), (5), (13), (27);
INSERT INTO tbl2 values(6), (14), (11), (20);
ANY關鍵字接在一個比較操做符的後面,表示若與子查詢返回的任何值比較爲TRUE,則返回TRUE。
【例7.53】返回tbl2表的全部num2列,而後將tbl1中的num1的值與之進行比較,只要大於num2的任何1個值,即爲符合查詢條件的結果。
SELECT num1 FROM tbl1 WHERE num1 > ANY (SELECT num2 FROM tbl2);
【例7.54】返回tbl1表中比tbl2表num2 列全部值都大的值,SQL語句以下:
SELECT num1 FROM tbl1 WHERE num1 > ALL (SELECT num2 FROM tbl2);
【例7.55】查詢suppliers表中是否存在s_id=107的供應商,若是存在,則查詢fruits表中的記錄,SQL語句以下:
SELECT * FROM fruits
WHERE EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);
【例7.56】查詢suppliers表中是否存在s_id=107的供應商,若是存在,則查詢fruits表中的f_price大於10.20的記錄,SQL語句以下:
SELECT * FROM fruits
WHERE f_price>10.20 AND EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);
【例7.57】查詢suppliers表中是否存在s_id=107的供應商,若是不存在則查詢fruits表中的記錄,SQL語句以下:
SELECT * FROM fruits
WHERE NOT EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);
【例7.58】在orderitems表中查詢f_id爲c0的訂單號,並根據訂單號查詢具備訂單號的客戶c_id,SQL語句以下:
SELECT c_id FROM orders WHERE o_num IN
(SELECT o_num FROM orderitems WHERE f_id = 'c0');
SELECT o_num FROM orderitems WHERE f_id = 'c0';
能夠看到,符合條件的o_num列的值有兩個:30003和30005,而後執行外層查詢,在orders表中查詢訂單號等於30003或30005的客戶c_id。嵌套子查詢語句還能夠寫爲以下形式,實現相同的效果:
SELECT c_id FROM orders WHERE o_num IN (30003, 30005);
【例7.59】與前一個例子相似,可是在SELECT語句中使用NOT IN關鍵字,SQL語句以下:
SELECT c_id FROM orders WHERE o_num NOT IN
(SELECT o_num FROM orderitems WHERE f_id = 'c0');
SELECT * FROM orders;
【例7.60】在suppliers表中查詢s_city等於「Tianjin」的供應商s_id,而後在fruits表中查詢全部該供應商提供的水果的種類,SQL語句以下:
SELECT s_id, f_name FROM fruits
WHERE s_id =
(SELECT s1.s_id FROM suppliers AS s1 WHERE s1.s_city = 'Tianjin');
【例7.61】在suppliers表中查詢s_city等於「Tianjin」的供應商s_id,而後在fruits表中查詢全部非該供應商提供的水果的種類,SQL語句以下:
SELECT s_id, f_name FROM fruits
WHERE s_id <>
(SELECT s1.s_id FROM suppliers AS s1 WHERE s1.s_city = 'Tianjin');
【例7.62】查詢全部價格小於9的水果的信息,查詢s_id等於101和103全部的水果的信息,使用UNION鏈接查詢結果,SQL語句以下:
SELECT s_id, f_name, f_price
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price
FROM fruits
WHERE s_id IN(101,103);
如前所述,UNION將多個SELECT語句的結果組合成一個結果集合。能夠分開查看每一個SELECT語句的結果:
SELECT s_id, f_name, f_price
FROM fruits
WHERE f_price < 9.0;
+------+-----------+---------+
| s_id | f_name | f_price |
+------+-----------+---------+
| 101 | apple | 5.20 |
| 103 | apricot | 2.20 |
| 104 | berry | 7.60 |
| 107 | xxxx | 3.60 |
| 105 | melon | 8.20 |
| 101 | cherry | 3.20 |
| 104 | lemon | 6.40 |
| 105 | xbabay | 2.60 |
| 102 | grape | 5.30 |
| 107 | xbababa | 3.60 |
+------+-----------+---------+
10 rows in set (0.00 sec)
SELECT s_id, f_name, f_price
FROM fruits
WHERE s_id IN(101,103);
+------+------------+---------+
| s_id | f_name | f_price |
+------+------------+---------+
| 101 | apple | 5.20 |
| 103 | apricot | 2.20 |
| 101 | blackberry| 10.20 |
| 101 | cherry | 3.20 |
| 103 | coconut | 9.20 |
+------+------------+---------+
【例7.63】查詢全部價格小於9的水果的信息,查詢s_id等於101和103的全部水果的信息,使用UNION ALL鏈接查詢結果,SQL語句以下:
SELECT s_id, f_name, f_price
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price
FROM fruits
WHERE s_id IN(101,103);
【例7.64】爲orders表取別名o,查詢30001訂單的下單日期,SQL語句以下:
SELECT * FROM orders AS o
WHERE o.o_num = 30001;
在這裏orders AS o代碼表示爲orders表取別名爲o,指定過濾條件時直接使用o代替orders,查詢結果以下:
+-------+---------------------+-------+
| o_num | o_date | c_id |
+-------+---------------------+-------+
| 30001 | 2008-09-01 00:00:00 | 10001 |
+-------+---------------------+-------+
【例7.65】爲customers和orders表分別取別名,並進行鏈接查詢,SQL語句以下:
SELECT c.c_id, o.o_num
FROM customers AS c LEFT OUTER JOIN orders AS o
ON c.c_id = o.c_id;
+-------+-------+
| c_id | o_num |
+-------+-------+
| 10001 | 30001 |
| 10001 | 30005 |
| 10002 | NULL |
| 10003 | 30002 |
| 10004 | 30003 |
+-------+-------+
由結果看到,MySQL能夠同時爲多個表取別名,並且表別名能夠放在不一樣的位置,如WHERE子句、SELECT列表、ON子句以及ORDER BY子句等。
在前面介紹內鏈接查詢時指出自鏈接是一種特殊的內鏈接,在鏈接查詢中的兩個表都是同一個表,其查詢語句以下:
SELECT f1.f_id, f1.f_name
FROM fruits AS f1, fruits AS f2
WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';
+------+------------+
| f_id | f_name |
+------+------------+
| a1 | apple |
| b1 | blackberry |
| c0 | cherry |
+------+------------+
【例7.66】查詢fruits表,爲f_name取別名fruit_name,f_price取別名fruit_price,爲fruits表取別名f1,查詢表中f_price < 8的水果的名稱,SQL語句以下:
SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price
FROM fruits AS f1
WHERE f1.f_price < 8;
【例7.67】查詢suppliers表中字段s_name和s_city,使用CONCAT函數鏈接這兩個字段值,並取列別名爲suppliers_title。
若是沒有對鏈接後的值取別名,其顯示列名稱將會不夠直觀,SQL語句以下:
SELECT CONCAT(TRIM(s_name) , ' (', TRIM(s_city), ')')
FROM suppliers
ORDER BY s_name;
+--------------------------------------------------------------+
| CONCAT(TRIM(s_name) , ' (', TRIM(s_city), ')') |
+--------------------------------------------------------------+
| ACME (Shanghai) |
| DK Inc. (Qingdao) |
| FastFruit Inc. (Tianjin) |
| FNK Inc. (Zhongshan) |
| Good Set (Taiyuan) |
| Just Eat Ours (Beijing) |
| LT Supplies (Chongqing) |
+---------------------------------------------------------------+
由結果能夠看到,顯示結果的列名稱爲SELECT子句後面的計算字段,實際上計算以後的列是沒有名字的,這樣的結果讓人很不容易理解,若是爲字段取一個別名,將會使結果清晰,SQL語句以下,
SELECT CONCAT(TRIM(s_name) , ' (', TRIM(s_city), ')')
AS suppliers_title
FROM suppliers
ORDER BY s_name;
+------------------------------+
| suppliers_title |
+------------------------------+
| ACME (Shanghai) |
| DK Inc. (Qingdao) |
| FastFruit Inc. (Tianjin) |
| FNK Inc. (Zhongshan) |
| Good Set (Taiyuan) |
| Just Eat Ours (Beijing) |
| LT Supplies (Chongqing)|
+------------------------------+
【例7.68】在fruits表中,查詢f_name字段以字母’b’開頭的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP '^b';
+------+------+-----------------+------------+
| f_id | s_id | f_name | f_price |
+------+------+-----------------+-------------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| t1 | 102 | banana | 10.30 |
+------+----------+---------------+-----------+
fruits表中有3條記錄的f_name字段值是以字母b開頭,返回結果有3條記錄。
【例7.69】在fruits表中,查詢f_name字段以「be」開頭的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP '^be';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b2 | 104 | berry | 7.60 |
+------+------+--------+---------+
只有berry是以「be」開頭,因此查詢結果中只有1條記錄。
7.8.2 查詢以特定字符或字符串結尾的記錄
字符’$’匹配以特定字符或者字符串結尾的文本。
【例7.70】在fruits表中,查詢f_name字段以字母’y’結尾的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP 'y$';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| c0 | 101 | cherry | 3.20 |
| m2 | 105 | xbabay | 2.60 |
+--------+--------+-------------+---------+
fruits表中有4條記錄的f_name字段值是以字母’y’結尾,返回結果有4條記錄。
【例7.71】在fruits表中,查詢f_name字段以字符串「rry」結尾的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP 'rry$';
+------+------+------------+-----------+
| f_id | s_id | f_name | f_price |
+------+------+------------+-----------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| c0 | 101 | cherry | 3.20 |
+------+------+-------------+------------+
fruits表中有3條記錄的f_name字段值是以字符串「rry」結尾,返回結果有3條記錄。
7.8.3 用符號"."來替代字符串中的任意一個字符
字符’.’匹配任意一個字符。
【例7.72】在fruits表中,查詢f_name字段值包含字母’a’與’g’且兩個字母之間只有一個字母的記錄,SQL語句以下,
SELECT * FROM fruits WHERE f_name REGEXP 'a.g';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1 | 102 | orange | 11.20 |
| m1 | 106 | mango | 15.60 |
+------+------+--------+---------+
查詢語句中’a.g’指定匹配字符中要有字母a和g,且兩個字母之間包含單個字符,並不限定匹配的字符的位置和所在查詢字符串的總長度,所以orange和mango都符合匹配條件。
7.8.4 使用"*"和"+"來匹配多個字符
星號’*’匹配前面的字符任意屢次,包括0次。加號’+’匹配前面的字符至少一次。
【例7.73】在fruits表中,查詢f_name字段值以字母’b’開頭,且’b’後面出現字母’a’的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP '^ba*';
+------+------+------------+------------+
| f_id | s_id | f_name | f_price |
+------+------+------------+------------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| t1 | 102 | banana | 10.30 |
+------+------+------------+--------------+
星號’*’能夠匹配任意多個字符,blackberry和berry中字母b後面並無出現字母a,可是也知足匹配條件。
【例7.74】在fruits表中,查詢f_name字段值以字母’b’開頭,且’b’後面出現字母’a’至少一次的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP '^ba+';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| t1 | 102 | banana | 10.30 |
+------+------+--------+---------+
‘a+’匹配字母’a’至少一次,只有banana知足匹配條件。
7.8.5 匹配指定字符串
正則表達式能夠匹配指定字符串,只要這個字符串在查詢文本中便可,如要匹配多個字符串,多個字符串之間使用分隔符’|’隔開。
【例7.75】在fruits表中,查詢f_name字段值包含字符串「on」的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP 'on';
+------+------+-----------+---------+
| f_id | s_id | f_name | f_price |
+------+------+-----------+---------+
| bs2 | 105 | melon | 8.20 |
| l2 | 104 | lemon | 6.40 |
| o2 | 103 | coconut | 9.20 |
+------+------+------------+---------+
能夠看到,f_name字段的melon、lemon和coconut3個值中都包含有字符串「on」,知足匹配條件。
【例7.76】在fruits表中,查詢f_name字段值包含字符串「on」或者「ap」的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP 'on|ap';
+------+------+----------+---------+
| f_id | s_id | f_name | f_price |
+-------+-------+-----------+---------+
| a1 | 101 | apple | 5.20 |
| a2 | 103 | apricot | 2.20 |
| bs2 | 105 | melon | 8.20 |
| l2 | 104 | lemon | 6.40 |
| o2 | 103 | coconut | 9.20 |
| t2 | 102 | grape | 5.30 |
+-------+-------+----------+----------+
【例7.77】在fruits表中,使用LIKE運算符查詢f_name字段值爲「on」的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name LIKE 'on';
Empty set (0.00 sec)
f_name字段沒有值爲「on」的記錄,返回結果爲空。讀者能夠體會一下二者的區別。
7.8.6 匹配指定字符中的任意一個
方括號「[]」指定一個字符集合,只匹配其中任何一個字符,即爲所查找的文本。
【例7.78】在fruits表中,查找f_name字段中包含字母’o’或者’t’的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_name REGEXP '[ot]';
+------+------+---------+---------+
| f_id | s_id | f_name | f_price |
+------+------+---------+---------+
| a2 | 103 | apricot | 2.20 |
| bs1 | 102 | orange | 11.20 |
| bs2 | 105 | melon | 8.20 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| m3 | 105 | xxtt | 11.60 |
| o2 | 103 | coconut | 9.20 |
+------+------+---------+---------+
查詢結果能夠看到,全部返回的記錄的f_name字段的值中都包含有字母o或者t,或者兩個都有。
方括號「[]」還能夠指定數值集合
【例7.79】在fruits表,查詢s_id字段中數值中包含四、5或者6的記錄,SQL語句以下:
SELECT * FROM fruits WHERE s_id REGEXP '[456]';
+------+------+---------+----------+
| f_id | s_id | f_name | f_price |
+-------+-------+---------+---------+
| b2 | 104 | berry | 7.60 |
| bs2 | 105 | melon | 8.20 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| m2 | 105 | xbabay | 2.60 |
| m3 | 105 | xxtt | 11.60 |
+-------+-------+---------+----------+
查詢結果中,s_id字段值中有3個數字中的1個即爲匹配記錄字段。
匹配集合「[456]」也能夠寫成「[4-6]」即指定集合區間。例如「[a-z]」表示集合區間爲從a~z的字母,「[0-9]」表示集合區間爲全部數字。
7.8.7 匹配指定字符之外的字符
「[^字符集合]」匹配不在指定集合中的任何字符。
【例7.80】在fruits表中,查詢f_id字段包含字母a~e和數字1~2之外的字符的記錄,SQL語句以下:
SELECT * FROM fruits WHERE f_id REGEXP '[^a-e1-2]';
+------+------+---------+---------+
| f_id | s_id | f_name | f_price |
+------+------+---------+---------+
| 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.60 |
| 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 | 3.60 |
+------+------+---------+---------+
【例7.81】在fruits表中,查詢f_name字段值出現字母’x’至少2次的記錄,SQL語句以下: SELECT * FROM fruits WHERE f_name REGEXP 'x{2,}';+------+------+--------+---------+| f_id | s_id | f_name | f_price |+------+------+--------+---------+| b5 | 107 | xxxx | 3.60 || m3 | 105 | xxtt | 11.60 |+------+-------+--------+---------+能夠看到,f_name字段的「xxxx」包含了4個字母’x’,「xxtt」包含兩個字母’x’,均爲知足匹配條件的記錄。【例7.82】在fruits表中,查詢f_name字段值出現字符串「ba」最少1次,最多3次的記錄,SQL語句以下: SELECT * FROM fruits WHERE f_name REGEXP 'ba{1,3}';+------+----------+-----------+---------+| f_id | s_id | f_name | f_price |+------+----------+----------+------------+| m2 | 105 | xbabay | 2.60 || t1 | 102 | banana | 10.30 || t4 | 107 | xbababa | 3.60 |+-------+--------+------------+-----------+能夠看到,f_name字段的xbabay值中「ba」出現了2次,banana中出現了1次,xbababa中出現了3次,都知足匹配條件的記錄。