mysql 怎么算第二天_mysql 第二天

-- 条件查询:select from where

-- 查询客户a1的购买记录

select *

from fruit

where s_name = ‘a1‘;

#或者可以查询到a1的购买次数

select count(distinct s_name) from fruit; #其中distinct 为去除重复的项目

-- 查询客户a2购买的价格小于10的水果

select f_name , f_id

from fruit

where s_name = ‘a2‘ and f_price <10;

-- 查询价格在10到20之间的水果 #in 查询

##方法一

select *

from fruit

where f_price in

(select f_price

from fruit

where f_price between 10 and 20);

##方法二

select *

from fruit

where f_price >=10 and f_price <=20;

-- 空值查询 select from where is null;

-- 查询f_price 为空的记录

select *

from fruit

where f_price is null ; #判断为空的值不能用等号,,用is

-- 查询价格不为空的所有记录

select *

from fruit

where f_price is not null;

-- 模糊查询 select from where like

-- 查询名称以a开头的水果记录;

select *

from fruit

where f_name like "a%";

-- 查询名称中包含a的水果记录

select *

from fruit

where f_name like "%a%"; #名称中第二个字符为a的水果记录有 like ‘_a%‘

--

查询,聚合运算,分组查询等

-- 聚合运算

-- 查询fruit表中所有水果的数量、最高价、最低价和平均价格

select count(f_id),max(f_price),min(f_price),avg(f_price)

from fruit;

-- 分组查询

-- 查询每位客户购买的水果均价

select s_name,avg(f_price)

from fruit

group by s_name;

-- 查询每位客户购买的不同水果的平均价格

select s_name , f_name , avg(f_price)

from fruit

group by s_name,f_name;

-- 分组后筛选:select 字段1[,字段2,…] from 表名[ where 查询条件][ group by 分组字段1[,分组字段2,…]] having 筛选条件;

-- 查询客户a1购买的水果均价

#第一种方法

select s_name,f_name,avg(f_price)

from fruit

where s_name = ‘a1‘;

#第二种方法

select s_name ,f_name, avg(f_price)

from fruit

group by s_name

having s_name= ‘a1‘;

注意:代码执行的顺序,where ,group by ,having ,order by

-- 分组前筛选where

-- 客户购买水果均价大于10 ##只有真实存在的数据可以在where中使用,计算得到的不可以在where中用,,可以having中

select s_name,f_name,avg(f_price)

from fruit

group by s_name

having avg(f_price) >10;

-- having在分组和聚合之后筛选,而where子句不能包含聚合函数,having 做使用的子句必须在select 中出现

-- 查询购买苹果的客户

select s_name ,f_name

from fruit

where f_name = ‘apple‘

group by s_name;

-- having 子句中的筛选字段必须在 select 子句中出现

-- 查询每位客户购买的水果中apple的均价

###注意:先where然后是group by 、 order by 、 having ,,,所以此时要显示客户购买苹果均价时应该以水果进行分组,而不是以客户进行分组

select s_name,f_name,avg(f_price)

from fruit

group by f_name

having f_name = ‘apple‘;

-- 查询结果排序:select 字段1[,字段2,…] from 表名 order by 字段1[ 排序方向,字段2 排序方向,…];

-- 单字段排序

-- 查询所有水果信息按f_price降序显示

select *

from fruit

order by f_price desc;

-- 多字段排序:先按第一个字段排序,第一个字段值相同时再按第二个字段排序

-- 查询所有水果信息按s_name升序、f_price降序显示 意义:可以看出同一位用户所购物品的最大至最小

select *

from fruit

order by s_name,f_price desc;

-- 排序字段中的null默认排在最前面

-- 查询所有水果信息按f_price升序显示

select *

from fruit

order by f_price asc;

-- 限制查询结果数量:select 字段1[,字段2,…] from 表名 limit [偏移量,] 行数 | 行数 offset 偏移量; 用limit函数

-- 查询f_price最高的十种水果

select *

from fruit

order by f_price desc

limit 10;

-- 查询f_price排在第10以后的5种水果

select *

from fruit

order by f_price desc

limit 10,5; ##limit 1,10 第二道第11位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值