mysql查询cp值数据_mysql的查询、子查询及连接查询

一、mysql查询的五种子句

where(条件查询)、having(筛选)、group by(分组)、order by(排序)、limit(限制结果数)

1、where常用运算符:

比较运算符

> ,  < ,=  , != (< >),>=   ,   <=

in(v1,v2..vn)

mysql> select id,name from user where id in(7,9,11,100);

+----+-----------+

| id | name |

+----+-----------+

| 7 | 娲紵楣? |

| 9 | CCC |

| 11 | 闄堥摥閽? |

+----+-----------+

3 rows in set (0.02 sec)

between v1 and v2    在v1至v2之间(包含v1,v2)

逻辑运算符

not ( ! )  逻辑非

or ( || )    逻辑或

and ( && )  逻辑与

where price>=3000 and price <= 5000 or price >=500 and price <=1000

取500-1000或者3000-5000的值

where price not between 3000 and 5000

不在3000与5000之间的值

模糊查询

like 像

通配符:

%  任意字符

_   单个字符

where goods_name like '诺基亚%'

where goods_name like '诺基亚N__'(必须要两个字符 ‘诺基亚N85’ ,诺基亚N9 就不行) 要匹配一个、两个字符的话不嫌麻烦的要OR把

2、group by 分组

例子 aa表 a b

123 10

123 12

1234 11

1234 14

首先 group 是用来分组的 不是过滤重复项的。重复项删除语句 DISTINCT用这个 。 select DISTINCT(a) from aa

结果就是 a

123

1234

group by用来分组的

select a, sum(b) from aa group by a

sum意思是总和。结果就是

a b

123 22

1234 25

语句的目的是以a为目标 需要知道 相同名字的物品 在b列一共有多少数量总和

select a,count(b) from aa group by a

count 意思行数总和 结果就是

a b

123 2

1234 2

语句目的是 相同名字的物品 一共有几行

(1)max:求最大值

select max(goods_price) from goods

这里会取出最大的价格的值,只有值

#查询每个栏目下价格最高的

select cat_id,max(goods_price) from goos group by cat_id;

#查出价格最高的商品编号

select goods_id,max(goods_price) from goods group by goods_id;

(2)min:求最小值

(3)sum:求总数和

#求商品库存总和

select sum(goods_number) from goods;

(4)avg:求平均值

#求每个栏目的商品平均价格

select cat_id,avg(goods_price) from goods group by cat_id;

(5)count:求总行数

#求每个栏目下商品种类

select cat_id,count(*) from goods group by cat_id;

以ustuid为目标 统计apply表里每个ustuid有多少记录(用group by  结果有排序的效果(如下ustuid))

mysql> select ustuid,count(*) from apply group by ustuid;

+-----------+----------+

| ustuid | count(*) |

+-----------+----------+

| 031302305 | 3 |

| 031302331 | 11 |

| 031302605 | 3 |

| 031302625 | 1 |

| 031302642 | 2 |

| 123123123 | 3 |

| 221300300 | 1 |

| 221300313 | 2 |

| 221300410 | 1 |

| 313 | 2 |

| 321321321 | 3 |

+-----------+----------+

11 rows in set (0.09 sec)

###要把每个字段名当成变量来理解,它可以进行运算###

例:查询本店每个商品价格比市场价低多少;

select goods_id,goods_name,goods_price-market_price from goods;

查询每个栏目下面积压的货款

select cat_id,sum(goods_price*goods_number) from goods group by cat_id;

###可以用as来给计算结果取个别名###

select cat_id,sum(goods_price * goods_number)  as hk from goods group by cat_id

不仅列名可以取别名,表单也可以取别名

3、having 与where 的异同点

having与where类似,可以筛选数据,where后的表达式怎么写,having后就怎么写

where针对表中的列发挥作用,查询数据

having对查询结果中的列发挥作用,筛选数据

mysql> select id,stuid,score as s from user;

+----+-----------+------+</

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值