目录
9、查询成本大于3元并小于40元的产品信息,并按照成本从高到低的顺序显示结果。
1、创建数据库并进入。
create database shop;
use shop;
2、创建远程数据库的操作用户
grant all on shop.* to 'testuser'@'192.168.115.%' identified by '123.com';
3、刷新数据库
flush privileges;
4、客户端远程登陆服务器
yum -y install mariadb
mysql -uadmin -p123.com -h192.168.115.151
5、创建数据表products
create table products(名称 varchar(50), 种类 varchar(50), 成本 varchar(50), 出厂日期 var
char(50)) default character set utf8;
6、 数据表添加内容
insert into products
value
('西瓜','水果','4.1000','2017-05-05 00:00:00.000'),
('芹菜','蔬菜','1.0000','2017-04-01 00:00:00.000'),
('番茄','蔬菜','2.9000','2017-05-09 00:00:00.000'),
('黄瓜','蔬菜','2.2000','2017-05-05 00:00:00.000'),
('香蕉','水果','6.1000','2017-05-23 00:00:00.000'),
('核桃','坚果','28.5000','2017-03-03 00:00:00.000'),
('开心果','坚果','38.1100','2017-02-22 00:00:00.000'),
('蓝莓','水果','50.2000','2017-05-11 00:00:00.000');
select * from products;
7、查询成本低于10元的水果信息
select * from products where 成本 < 10 and 种类='水果';
8、将所有蔬菜的成本上调1元。
update products set 成本=成本+'1.0000' where 种类='蔬菜';
9、查询成本大于3元并小于40元的产品信息,并按照成本从高到低的顺序显示结果。
select * from products where 成本 > 3 and 成本 < 40 order by 成本 DESC;
10、查询成本最高的5个产品信息。
select * from products where 成本 order by 成本 desc limit 5;
11、查询有哪些产品种类。
select distinct 种类 from products;
12、查询出厂日期晚于2017年4月的水果信息
select * from products where 出厂日期 > '2017-04' and 种类='水果';
13、分组查询所有水果、蔬菜、坚果的总成本。
select 种类,sum(成本) from products where 种类 in ('蔬菜','水果','坚果') group by 种类;
14、查询所有蔬菜的平均成本。
select avg(成本) from products where 种类='蔬菜';