– select * from cate;
– select * from goods;
– select * from brand;
– -- #1,查询商品以及商品对应的品牌名
select goods.name,brand.brandename
from goods
inner join brand
where goods.brande_id=brand.id
– #查询商品价格大于9.9的商品以及对应的商品名
select g.name,b.brandename
from goods as g,brand as b
where g.price>9.9 and g.brande_id=b.id;
– #查询每个品牌的商品的个数并且展示品牌的名字
select count(*),b.brandename
from goods as g,brand as b
where g.brande_id=b.id
group by g.brande_id;
– 99语法规则
‘’’
select 查询字段
from 表名1 join 表名2
on 连接条件
where 筛选条件
group by 字段名
order by 字段名 asc|desc
‘’’
一、WHERE条件筛选
SELECT * FROM goods
SELECT goods.name,goods.price FROM goods;
SELECT goods.name
AS ‘商品名’,goods.price
AS ‘价格’ FROM goods;
SELECT goods.name
‘商品名’,goods.price
‘价格’ FROM goods;
SELECT DISTINCT goods.name AS ‘商品名’ FROM goods
找出所有单价大于9.9的商品
SELECT goods.name FROM goods WHERE goods.price
>9.9
找出价格小于9.9 ,已经上架的产品
SELECT goods.name FROM goods WHERE goods.price <9.9 AND goods.is_show =1
找出商品名中带火的商品
SELECT goods.name
FROM goods WHERE goods.name LIKE ‘%火%’
SELECT goods.name
FROM goods WHERE goods.name LIKE ‘火%’
SELECT goods.name
FROM goods WHERE goods.name LIKE ‘%火’
SELECT goods.name
FROM goods WHERE goods.name LIKE ‘火__’
找出价格区间在10-100之间的商品
SELECT * FROM goods WHERE goods.price
BETWEEN 10 AND 100;
找出id 是 3 5 7的商品
SELECT * FROM goods WHERE id IN(13,15,17);
SELECT * FROM userinfo WHERE age IS NULL
/*
where 支持的运算符
1、比较运算符
> < >= <= = !=
2、逻辑运算符
and not or
3、模糊查询
like
%任意多个 _任何一个字符
4、范围查找:
连续范围: BETWEEN AND
不连续: in
5、空值
is null
6、优先级
小括号 not 比较运算符、逻辑运算符
and 比or先运行,如果希望先运算or使用()
*/
– ======================= 排序====================================
将查询出来的大于9.9的[商品]按照价格进行排序(升序)(降序)
SELECT * FROM goods WHERE goods.price >9.9 ORDER BY goods.price
DESC,goods.id desc;
/*
格式:
select * from 表名 [筛选条件(可以有可以无)] order by 字段1 asc|desc,字段2 asc|desc…
1、将数据按照字段1进行排序,如果某些行的字段1的数值是相同的,按照字段2进行排序。
2、默认的排序方式是升序
3、asc升序 ,desc降序
*/
聚合函数(快速统计)============
查询总数
SELECT COUNT(*) FROM goods
查询出商品的最高价格
SELECT MAX(price) FROM goods
找出最低价格
SELECT MIN(price) FROM goods
求商品的平均价格
SELECT AVG(price) FROM goods
不使用内置的avg
SELECT SUM(price)/COUNT(*) AS 均价 FROM goods
– 分组==
将数据按照种类分组之后进行展示
SELECT cate_id,GROUP_CONCAT(NAME) FROM goods GROUP BY cate_id;
找出按照种类进行分组之后,每一个分组的平均价格
SELECT cate_id,AVG(price) FROM goods GROUP BY cate_id
#按照种类进行分组,筛选出平均价格大于9.9的商品
SELECT cate_id ,GROUP_CONCAT(NAME),AVG(price) FROM goods GROUP BY cate_id HAVING AVG(price)>9.9
SELECT cate_id,AVG(price) FROM goods GROUP BY cate_id WITH ROLLUP
/*
group by
意义:将查询结果按照一个或者多个字段进行分组,字段值相同的是一组数据
group_concat:作为一个输出字段,用来放置每一组当前字段的值的集合
select 分组字段,group_concat(需要展示的字段名–值的集合) from 表名 [筛选条件] group by 分组字段
聚合函数的使用,配合分组做统计count avg
having
where 条件筛选,目前用where 筛选原来的表中的原始数据
having 条件筛选用于group by
WITH ROLLUP:在group分组字段的基础上再进行统计数据
*/
– =分页==========
SELECT * FROM goods LIMIT 0,3
/*
limit
limit start,count
起始位置,获取count条数据
*/
– =多表查询======
#1、查询商品以及商品对应的品牌名
SELECT goods.name,brand.brandename
FROM goods,brand
SELECT COUNT() FROM brand
笛卡尔乘积:
A表中有M条数据,B表中有N条数据,最终结果是MN条数据
错误原因:多张表进行查询的时候没有遵循指定连接条件
SELECT * FROM goods,brand WHERE goods.brande_id
=brand.id
按照年代
sql92
sql99 内连接 外连接(左右外连接,mysql全外连接是不支持)交叉连接
SELECT g.name
商品名 ,b.brandename 品牌名
FROM goods AS g,brand AS b
WHERE g.brande_id
=b.id
查询商品价格大于9.9 的商品名以及对应的品牌名
SELECT g.name
,g.price,b.brandename
FROM goods g,brand b
WHERE g.price>9.9 AND g.brande_id=b.id
查询每个品牌的商品的个数并且展示品牌的名字
SELECT COUNT(*),b.brandename
FROM goods g,brand b
WHERE g.brande_id=b.id
GROUP BY g.brande_id
查询每个品牌的商品的个数并且展示品牌的名字,按照均价排序
SELECT COUNT(*),b.brandename
FROM goods g,brand b
WHERE g.brande_id=b.id
GROUP BY g.brande_id
ORDER BY AVG(g.price)
展示商品的名字对应的种类名以及品牌名
SELECT goods.name
,cate.name
,brand.brandename
FROM goods,cate,brand
WHERE goods.cate_id
=cate.id
AND goods.brande_id
=brand.id
查询商品价格和价格对应的区间等级
SELECT goods.name
,goods. price, price_grade.price_level
FROM goods ,price_grade
WHERE goods.price
BETWEEN price_grade.min_price
AND price_grade.max_price
/*
sql99语法规则
select 查询字段
from 表名1 join 表名2
on 连接条件
where 原始筛选条件
group by …
having…
order by
*/
– 查询商品名以及对应的种类
SELECT goods.name
,cate.name
FROM goods
INNER JOIN cate
ON goods.cate_id
=cate.id
查询名字中带火的商品以及对应的品牌名
SELECT goods.name
,brand.brandename
FROM goods
INNER JOIN brand
ON goods.brande_id
= brand.id
WHERE goods.name
LIKE ‘%火%’
查询商品个数大于3个的品牌的个数并且展示品牌的名字
SELECT COUNT() FROM goods GROUP BY goods.brande_id
HAVING COUNT()>3
SELECT goods.brande_id
,COUNT(),brand.brandename
FROM goods
INNER JOIN brand
ON goods.brande_id
= brand.id
GROUP BY goods.brande_id
HAVING COUNT() >3
#统计每个品牌的各个种类的商品
SELECT goods.brande_id,goods.cate_id,GROUP_CONCAT(NAME) FROM goods GROUP BY goods.brande_id,good.cate_id
– 左连接
/*
以左表为主表,查询出右表与它匹配的内容,如果有合并成一条数据,如果没有,则空值填充;
外连接的查询结果=内连接的查询结果+主表有儿从表没有的数据
*/
SELECT * FROM goods LEFT JOIN brand ON goods.brande_id=brande_id
SELECT * FROM brand LEFT JOIN goods ON goods.brande_id=brande_id
#三表连接
SELECT * FROM (goods INNER JOIN cate ON goods.cate_id=cate_id) INNER JOIN brand ON goods.brande_id=brande_id;
#自关联
– 表中的一列关联了这个表中的另外一列
SELECT * FROM areas;