MySQL数据库的基础练习

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 MySQL数据库的基础练习,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

题目:按要求创建一个数据库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);

1a6e493765e522f381ed0e41f0ad4902.png

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');

183f8a56a3d793653d210aede5cdd921.png

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);

c43d03c987571a1c287c888065520588.png

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);

5d3452ef34b58199d13411683d2a42b4.png

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');

2bc4752da65ccc8ab5fe40b61a6e6646.png


一、单表查询 基础练习

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;

282bc753599f1eeea2127812742d0b9d.png

2.查询fruits表中f_name列所有水果名称,SQL语句如下:

select f_name from fruits;

bade5f32ab275b3780181b8aeeb33ba9.png

3.从fruits表中获取f_name和f_price两列,SQL语句如下:

select f_name,f_price from fruits;

2ebc4960ee5315611ef11253c89bfaad.png

4.查询价格为8.2元的水果的名称

select f_name from fruits where f_price=8.2;

feda8283c276af4bcbc1e14cc7d46d89.png

5.查找名称为“apple”的水果的价格,SQL语句如下:

select f_price from fruits where f_name='apple';  一定要注意要用单引号扩上!

7dca70fde5af8636d2edf5f165340195.png

6.查询价格小于8的水果的名称

select f_name from fruits where f_price<8;

96a8d389977cea4b338b39acdbcd57e1.png

7.查询s_id为81和82的记录,并按照f_name升序排序,SQL语句如下:

用到inSELECT s_id FROM fruits WHERE s_id IN (81,82) ORDER BY f_name;

7d2dd439c9201bd4ab6ccb69cd172f12.png

8.查询所有s_id不等于81也不等于82的记录,并按照f_name升序排序

select * from fruits where s_id<>81 and s_id<>82 order by f_name;

63f66f2872eeb6cc19bf15cb1cec646b.png

9.查询价格在2.00元到8.20元之间的水果名称和价格。

select f_name,f_price from fruits where f_price between 2.00 and 8.20;

8eb0456a4a1fd84be2bda9361185c4cf.png

10.查询价格在2.00元到8.20元之外的水果名称和价格。

select f_name,f_price from fruits where f_price not between 2.00 and 8.20;

fa9ea4562f5a69374447e7a3494f4969.png

11.查找所有以’b’字母开头的水果。

select * from fruits where f_name like'b%';

2432e5ee01dd0c58092fe87e8a0e3854.png

12.在fruits表中,查询f_name中包含字母’g’的记录。

select *  from  fruits where  f_name  like  '%g%';

2caa15b3aaf68698ea6e2275e4aaed94.png

13.查询以’b’开头,并以’y’结尾的水果的名称。

select * from fruits where f_name like 'b%y';

fbc2ec9c31091e3d0e72704da41a9c72.png

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;

44294ededc9794aab038a81a5124bbe9.png

15.在fruits表中查询s_id = 81,并且f_price大于等于5的水果价格和名称。

select f_price,f_name from fruits where s_id=81 and f_price>=5;

fe6f11e81afbbcd3f6f5e63675a23793.png

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';

94d3ce3c6e57441801368d2a0f168c39.png

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;

0f36a2b0e7abe5143be91dc387e598ac.png

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;

2113a93fdb991202c0bdffae5c54ae81.png

19.查询fruits表中s_id字段的值,返回s_id字段值且不得重复。

select distinct s_id from fruits;

4049a7c120f8529c85c90bcd5d29cae5.png

20.查询fruits表的f_name字段值,并对其进行排序。

select f_name from fruits order by f_name;

5f8eeb2fe1965d1fa79a4b23149f4924.png

21.查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序。

select f_name,f_price from fruits order by f_name,f_price;

d29a1894e7e0caf1ce629f82df98fda2.png

22.查询fruits表中的f_name和f_price字段,对结果按f_price降序方式排序。

select f_name,f_price from fruits order by f_price desc;

fbb7cdbe5ed5fcf6f2dd09a1f54c34ca.png

23.查询fruits表,先按f_price降序排序,再按f_name字段升序排序。

select f_price,f_name from fruits order by f_price desc,f_name;

MySQL数据库的基础练习

 

24.根据s_id对fruits表中的数据进行分组。

select s_id count(*) as total from fruits group by s_id;

fdb439629b31c60d878d0909c2a9de5c.png

25.根据s_id对fruits表中的数据进行分组,将每个供应商的水果名称显示出来。

SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id WITH ROLLUP;

e337d1232d77136e3ef7fa139af72f90.png

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;

774842e647b3591ed52298a25c0e3fc8.png

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;

5069f30dd23974386f1fcd53b9aa78e0.png

以id分组,把name字段的值打印在一行,分号分隔

select s_id,group_concat(f_name separator ';') from fruits group by s_id;

f9202aae77fff621b08557eb44cab846.png

以id分组,把去冗余的name字段的值打印在一行, 逗号分隔

select s_id,group_concat(distinct f_name) from fruits group by s_id;

336001eadd84d4c42f5920a41da5d0c3.png

以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序

select s_id,group_concat(f_name order by f_name desc) from fruits group by s_id;

a42ea7b2d9e0262f0a853bff62651e82.png

28.根据s_id对fruits表中的数据进行分组,并显示记录数量。

select s_id, count(*) from fruits group by s_id with rollup;

ec1077424f95fa794a0a816febc4fb36.png

29.根据s_id和f_name字段对fruits表中的数据进行分组。

select s_id & f_name from fruits group by s_id & f_name;

fa6e0efed68e0326ad0a198552d94598.png

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;

7da7dceb8e4d3c1b94bff7b4f565e8d6.png

caad8729cf2145f42709a7e71fc8e974.png

31.显示fruits表查询结果的前4行。

select * from fruits limit 4;

5e09ea8ad1d145dca46a142d43db1a50.png

32.在fruits表中,使用LIMIT子句,返回从第5个记录开始的,行数长度为3的记录。

select * from fruits limit 4,3;

50187bb17ba8fb3c1050ac9946288c1b.png

33.查询customers1表中总的行数

select count(*) as goods_num  from customers;

19817106c91f64cc0d7024b1ea6d612b.png

34.查询customers1表中有电子邮箱的顾客的总数查询customers1表中有电子邮箱的顾客的总数。

select count(c_email) as email_num from customers;

9b2d4729c83501517b4ab7a7ebacb6ae.png

35.在orderitems表中,使用COUNT()函数统计不同订单号中订购的水果种类。

select count(f_id) as o_num from fruits;

181cd62594783047ee9f42886092cf01.png

36.在orderitems表中,使用SUM()函数统计不同订单号中订购的水果总量。

select  sum(quantity)  from orderitems;

d193a1e3cc74db63d5315a5d462a5f5b.png

37.在fruits表中,查询s_id=83的供应商的水果价格的平均值。

select avg(f_price) as avg_price from fruits  where s_id = 83;

6e6a0b0eb8a4337497d92bf54ec61e47.png

38.在fruits表中,查询每一个供应商的水果价格的平均值。

select s_id,avg(f_price) as avg_price from fruits group by s_id;

6a70ccdde9db57de82412687b14de260.png

39.在fruits表中查找市场上价格最高的水果。

 select max(f_price) from fruits;

fdd02e2b1dd2ffcfdf5e1add3ceb03f9.png

40.在fruits表中查找不同供应商提供的价格最高的水果。

select distinct s_id,max(f_price) minpri  from fruits group by s_id;

2644b97dc8c4c109a3bcf8f2df0e575c.png

41.在fruits表中查找f_name的最大值。

select max(f_name) from fruits;

22c9b884d3266833bc3a264a56cfc996.png

42.在fruits表中查找市场上价格最低的水果。

select min(f_name) from fruits;

fd47f8f82bca58b998bb2ea259d7d4a4.png

43.在fruits表中查找不同供应商提供的价格最低的水果。

select distinct s_id,min(f_price) minpri  from fruits group by s_id;

f94cb8413ef60be629742aff51891b5d.png

注意:where 子句和 having 子句都具有按照条件筛选数据的功能,两者之间的区别主要有:

where 子句再进行分组操作前用来选择记录,而having 子句在进行分组操作之后通过过滤来选择分组;

having 子句中的每个字段必须包含在select 关键字后的字段列表中;

having 子句可以包含聚合函数,但是where 子句不能。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/130061.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!