题目:按要求创建一个数据库dblx1,然后在该数据库下创建几个表。
1.新建数据表fruitsCREATE TABLE fruits(f_id char(8) NOT NULL,s_id INT NOT NULL,f_name char(255) NOT NULL,f_price decimal(10,2) NOT NULL,PRIMARY KEY(f_id) );INSERT INTO fruits (f_id, s_id, f_name, f_price) VALUES('a1', 81,'apple',5.2), ('b1',81,'blackberry', 8.2), ('bs1',82,'orange', 11.2), ('bs2',85,'melon',8.2), ('t1',82,'banana', 8.3), ('t2',82,'grape', 5.3), ('o2',83,'coconut', 9.2), ('c0',81,'cherry', 3.2), ('a2',83, 'apricot',2.2), ('l2',84,'lemon', 6.4), ('b2',84,'berry', 7.6), ('m1',86,'mango', 15.6), ('m2',85,'xbabay', 2.6), ('t4',87,'xbababa', 3.6), ('m3',85,'xxtt', 11.6), ('b5',87,'xxxx', 3.6);
2.创建数据表suppliersCREATE TABLE suppliers( s_id int NOT NULL AUTO_INCREMENT, s_name char(50) NOT NULL, s_city char(50) NULL, s_zip char(8) NULL, s_call CHAR(50) NOT NULL, PRIMARY KEY (s_id)) ;INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call)VALUES(81,'FastFruit Inc.','Tianjin','300000','48075'),(82,'LT Supplies','Chongqing','400000','44333'),(83,'ACME','Shanghai','200000','90046'),(84,'FNK Inc.','Zhongshan','528437','11111'),(85,'Good Set','Taiyuang','030000', '22222'),(86,'Just Eat Ours','Beijing','08', '45678'),(87,'DK Inc.','Zhengzhou','450000', '33332');
3.创建表ordersCREATE 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 orders(o_num, o_date, c_id)VALUES(30001, '2008-09-01', 8001),(30002, '2008-09-12', 8003),(30003, '2008-09-30', 8004),(30004, '2008-8-03', 8005),(30005, '2008-8-08', 8001);
4.新建数据表orderitemsCREATE TABLE orderitems( o_num int NOT NULL, o_item int NOT NULL, f_id char(8) NOT NULL, quantity int NOT NULL, item_price decimal(8,2) NOT NULL, PRIMARY KEY (o_num,o_item)) ;INSERT INTO orderitems(o_num, o_item, f_id, quantity, item_price)VALUES(30001, 1, 'a1', 8, 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', 80, 8),(30004, 1, 'o2', 50, 2.50),(30005, 1, 'c0', 5, 8),(30005, 2, 'b1', 8, 8.99),(30005, 3, 'a2', 8, 2.2),(30005, 4, 'm1', 5, 14.99);
5.创建数据表customersCREATE 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(8) 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(8001, 'RedHook', '200 Street ', 'Tianjin', '300000', 'LiMing', 'LMing@163.com'),(8002, 'Stars', '333 Fromage Lane', 'Dalian', '116000', 'Zhangbo','Jerry@hotmail.com'),(8003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000', 'LuoCong', NULL),(8004, 'JOTO', '829 Riverside Drive', 'Haikou', '570000', 'YangShan', 'sam@hotmail.com');
一、单表查询 基础练习
1.在SELECT语句中指定所有字段
以fruits 表为例:select* from fruits;select {*|col_list} from table_name; 或者是直接查询指定表中的所有字段,|col_list}表示数据表中的字段列表。select f_id, s_id,f_name,f_price from fruits; select*from fruits;
2.查询fruits表中f_name列所有水果名称,SQL语句如下:
select f_name from fruits;
3.从fruits表中获取f_name和f_price两列,SQL语句如下:
select f_name,f_price from fruits;
4.查询价格为8.2元的水果的名称
select f_name from fruits where f_price=8.2;
5.查找名称为“apple”的水果的价格,SQL语句如下:
select f_price from fruits where f_name='apple'; 一定要注意要用单引号扩上!
6.查询价格小于8的水果的名称
select f_name from fruits where f_price<8;
7.查询s_id为81和82的记录,并按照f_name升序排序,SQL语句如下:
用到inSELECT s_id FROM fruits WHERE s_id IN (81,82) ORDER BY f_name;
8.查询所有s_id不等于81也不等于82的记录,并按照f_name升序排序
select * from fruits where s_id<>81 and s_id<>82 order by f_name;
9.查询价格在2.00元到8.20元之间的水果名称和价格。
select f_name,f_price from fruits where f_price between 2.00 and 8.20;
10.查询价格在2.00元到8.20元之外的水果名称和价格。
select f_name,f_price from fruits where f_price not between 2.00 and 8.20;
11.查找所有以’b’字母开头的水果。
select * from fruits where f_name like'b%';
12.在fruits表中,查询f_name中包含字母’g’的记录。
select * from fruits where f_name like '%g%';
13.查询以’b’开头,并以’y’结尾的水果的名称。
select * from fruits where f_name like 'b%y';
14.查询customers1表中c_email不为空的记录的c_id、c_name和c_email字段值。
select c_id,c_name,c_email from customers where c_email is not null;
15.在fruits表中查询s_id = 81,并且f_price大于等于5的水果价格和名称。
select f_price,f_name from fruits where s_id=81 and f_price>=5;
16.在fruits表中查询s_id = 81或者82,且f_price大于5,并且f_name=‘apple’的水果价格和名称。
select f_price,f_name from fruits where s_id=81 or s_id=82 and f_price>5 and f_name='apple';
17.查询s_id=81或者s_id=82的水果供应商的f_price和f_name。
select f_price,f_name from fruits where s_id=81 or s_id=82;
18.查询s_id=81或者s_id=82的水果供应商的f_price和f_name
select f_price,f_name from fruits where s_id=81 or s_id=82;
19.查询fruits表中s_id字段的值,返回s_id字段值且不得重复。
select distinct s_id from fruits;
20.查询fruits表的f_name字段值,并对其进行排序。
select f_name from fruits order by f_name;
21.查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序。
select f_name,f_price from fruits order by f_name,f_price;
22.查询fruits表中的f_name和f_price字段,对结果按f_price降序方式排序。
select f_name,f_price from fruits order by f_price desc;
23.查询fruits表,先按f_price降序排序,再按f_name字段升序排序。
select f_price,f_name from fruits order by f_price desc,f_name;
24.根据s_id对fruits表中的数据进行分组。
select s_id count(*) as total from fruits group by s_id;
25.根据s_id对fruits表中的数据进行分组,将每个供应商的水果名称显示出来。
SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;
26.根据s_id对fruits表中的数据进行分组,并显示水果种类大于1的分组信息。
select s_id, GROUP_CONCAT(f_name) as names from fruits group by s_id having count(f_name) > 1;
group_concat()函数的用法
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
27.以id分组,把name字段的值打印在一行,逗号分隔(默认)
select s_id,group_concat(f_name) from fruits group by s_id;
以id分组,把name字段的值打印在一行,分号分隔
select s_id,group_concat(f_name separator ';') from fruits group by s_id;
以id分组,把去冗余的name字段的值打印在一行, 逗号分隔
select s_id,group_concat(distinct f_name) from fruits group by s_id;
以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
select s_id,group_concat(f_name order by f_name desc) from fruits group by s_id;
28.根据s_id对fruits表中的数据进行分组,并显示记录数量。
select s_id, count(*) from fruits group by s_id with rollup;
29.根据s_id和f_name字段对fruits表中的数据进行分组。
select s_id & f_name from fruits group by s_id & f_name;
30.查询订单价格大于80的订单号和总订单价格。
SELECT o_num, SUM(quantity * item_price) AS orderTotalFROM orderitemsGROUP BY o_numHAVING SUM(quantity*item_price) >= 100;select o_num,sum(quantity * item_price) as ordertotal from orderitems group by o_num having sum(quantity*item_price) >= 80;
31.显示fruits表查询结果的前4行。
select * from fruits limit 4;
32.在fruits表中,使用LIMIT子句,返回从第5个记录开始的,行数长度为3的记录。
select * from fruits limit 4,3;
33.查询customers1表中总的行数
select count(*) as goods_num from customers;
34.查询customers1表中有电子邮箱的顾客的总数查询customers1表中有电子邮箱的顾客的总数。
select count(c_email) as email_num from customers;
35.在orderitems表中,使用COUNT()函数统计不同订单号中订购的水果种类。
select count(f_id) as o_num from fruits;
36.在orderitems表中,使用SUM()函数统计不同订单号中订购的水果总量。
select sum(quantity) from orderitems;
37.在fruits表中,查询s_id=83的供应商的水果价格的平均值。
select avg(f_price) as avg_price from fruits where s_id = 83;
38.在fruits表中,查询每一个供应商的水果价格的平均值。
select s_id,avg(f_price) as avg_price from fruits group by s_id;
39.在fruits表中查找市场上价格最高的水果。
select max(f_price) from fruits;
40.在fruits表中查找不同供应商提供的价格最高的水果。
select distinct s_id,max(f_price) minpri from fruits group by s_id;
41.在fruits表中查找f_name的最大值。
select max(f_name) from fruits;
42.在fruits表中查找市场上价格最低的水果。
select min(f_name) from fruits;
43.在fruits表中查找不同供应商提供的价格最低的水果。
select distinct s_id,min(f_price) minpri from fruits group by s_id;
注意:where 子句和 having 子句都具有按照条件筛选数据的功能,两者之间的区别主要有:
where 子句再进行分组操作前用来选择记录,而having 子句在进行分组操作之后通过过滤来选择分组;
having 子句中的每个字段必须包含在select 关键字后的字段列表中;
having 子句可以包含聚合函数,但是where 子句不能。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/130061.html