查询数据
下面以一个例子说明如何使用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;
【例1】从fruits表中检索所有字段的数据,SQL语句如下:
SELECT * FROM fruits;
【例2】查询fruits表中f_name列所有水果名称,SQL语句如下:
SELECT f_name FROM fruits;
该语句使用SELECT声明从fruits表中获取名称为f_name字段下的所有水果名称,指定字段的名称紧跟在SELECT关键字之后,查询结果如下:
SELECT f_name FROM fruits;
【例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;
【例4】查询价格为10.2元的水果的名称,SQL语句如下:
SELECT f_name, f_price
FROM fruits
WHERE f_price = 10.2;
【例5】查找名称为“apple”的水果的价格,SQL语句如下:
SELECT f_name, f_price
FROM fruits
WHERE f_name = 'apple';
【例6】查询价格小于10的水果的名称,SQL语句如下:
SELECT f_name, f_price
FROM fruits
WHERE f_price < 10;
【例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;
【例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;
【例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;
【例10】查询价格在2.00元到10.20元之外的水果名称和价格,SQL语句如下:
SELECT f_name, f_price
FROM fruits
WHERE f_price NOT BETWEEN 2.00 AND 10.20;
【例11】查找所有以’b’字母开头的水果,SQL语句如下:
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE 'b%';
【例12】在fruits表中,查询f_name中包含字母’g’的记录,SQL语句如下:
SELECT f_id, f_name
FROM fruits
WHERE f_name LIKE '%g%';
【例13】查询以’b’开头,并以’y’结尾的水果的名称,SQL语句如下:
SELECT f_name
FROM fruits
WHERE f_name LIKE 'b%y';
【例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;
【例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;
【例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;
【例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;
【例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';
【例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;
【例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);
【例21】查询fruits表中s_id字段的值,返回s_id字段值且不得重复,SQL语句如下:
SELECT DISTINCT s_id FROM fruits;
【例22】查询fruits表的f_name字段值,并对其进行排序,SQL语句如下:
SELECT f_name
FROM fruits ORDER BY f_name;
【例23】查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序,SQL语句如下:
SELECT f_name, f_price
FROM fruits ORDER BY f_name, f_price;
【例24】查询fruits表中的f_name和f_price字段,对结果按f_price降序方式排序,SQL语句如下:
SELECT f_name, f_price
FROM fruits ORDER BY f_price DESC;
【例25】查询fruits表,先按f_price降序排序,再按f_name字段升序排序,SQL语句如下:
SELECT f_price, f_name
FROM fruits ORDER BY f_price DESC, f_name;
【例26】根据s_id对fruits表中的数据进行分组,SQL语句如下:
SELECT s_id, COUNT(*) AS Total
FROM fruits GROUP BY s_id;
【例27】根据s_id对fruits表中的数据进行分组,将每个供应商的水果名称显示出来,SQL语句如下:
SELECT s_id, GROUP_CONCAT(f_name) AS Names
FROM fruits GROUP BY s_id;
【例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;
【例29】根据s_id对fruits表中的数据进行分组,并显示记录数量,SQL语句如下:
SELECT s_id, COUNT(*) AS Total
FROM fruits
GROUP BY s_id WITH ROLLUP;
【例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);
【例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;
【例32】显示fruits表查询结果的前4行,SQL语句如下:
SELECT * From fruits LIMIT 4;
【例33】在fruits表中,使用LIMIT子句,返回从第5个记录开始的,行数长度为3的记录,SQL语句如下:
SELECT * From fruits LIMIT 4, 3;
【例34】查询customers表中总的行数,SQL语句如下:
SELECT COUNT(*) AS cust_num
FROM customers;
【例35】查询customers表中有电子邮箱的顾客的总数,SQL语句如下:
SELECT COUNT(c_email) AS email_num
FROM customers;
【例36】在orderitems表中,使用COUNT()函数统计不同订单号中订购的水果种类,SQL语句如下:
SELECT o_num, COUNT(f_id)
FROM orderitems
GROUP BY o_num;
【例37】在orderitems表中查询30005号订单一共购买的水果总量,SQL语句如下:
SELECT SUM(quantity) AS items_total
FROM orderitems
WHERE o_num = 30005;
【例38】在orderitems表中,使用SUM()函数统计不同订单号中订购的水果总量,SQL语句如下:
SELECT o_num, SUM(quantity) AS items_total
FROM orderitems
GROUP BY o_num;
【例39】在fruits表中,查询s_id=103的供应商的水果价格的平均值,SQL语句如下:
SELECT AVG(f_price) AS avg_price
FROM fruits
WHERE s_id = 103;
【例40】在fruits表中,查询每一个供应商的水果价格的平均值,SQL语句如下:
SELECT s_id,AVG(f_price) AS avg_price
FROM fruits
GROUP BY s_id;
【例41】在fruits表中查找市场上价格最高的水果,SQL语句如下:
SELECT MAX(f_price) AS max_price FROM fruits;
【例42】在fruits表中查找不同供应商提供的价格最高的水果,SQL语句如下:
SELECT s_id, MAX(f_price) AS max_price
FROM fruits
GROUP BY s_id;
【例43】在fruits表中查找f_name的最大值,SQL语句如下:
SELECT MAX(f_name) FROM fruits;
【例44】在fruits表中查找市场上价格最低的水果,SQL语句如下:
SELECT MIN(f_price) AS min_price FROM fruits;
【例45】在fruits表中查找不同供应商提供的价格最低的水果,SQL语句如下:
SELECT s_id, MIN(f_price) AS min_price
FROM fruits
GROUP BY s_id;
超全面的测试IT技术课程,0元立即加入学习!有需要的朋友戳:
腾讯课堂测试技术学习地址
欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。