- where
1).比较运算符
< 小于 <= 小于等于 = 等于 > 大于 >= 大于等于 !=或<>不等于 in 在某集合内 between 在某范围内
in(值1,值2,..值n),等于值1->n任意一个,都行 想查询第4个或者第5个栏目的商品
select * from goods where cat_id in (4,5);
between 值1 and 值2 ,表示在值1和值2之间
select * from goods where shop_price between 2000 and 3000;
想买2000-5000之间的商品但是不能用between and
select * from goods where shop_price >= 3000 and shop_price <= 5000;
2).逻辑运算符号
not或! 逻辑非 or或||逻辑或 and或&&逻辑与
3).模糊查询
案例:想查找“诺基亚”开头的所有商品
like->像 %->通配任意字符 ‘_’单个字符
select * from goods where goods_name like '诺基亚';
select * from goods where goods_name like '诺基亚%';
select * from goods where goods_name like '诺基亚N_';
4).正则表达式
语法 where 列名 regexp '正则表达式' 宗旨:含有‘’引号里面的字段!
regexp '1.' 表示含有1后面跟任意字符的字段;
regexp '1000' 表示含有1000的字段 而like表示1000,只能后面再加_或%
regexp '1000|2000|3000'; 只要有其中任意一个字段就返回
regexp '[123]ton' 匹配含有任意一个数字 是[1|2|3]的缩写。
regexp '[^123]' 匹配除123以外的任意字符。
如果要区分大小写则用 regexp binary '正则表达式';
[123456789] 跟 [0-9] 都是匹配任意一个字符
\\表示转义后面跟需要的符号 \\.表示查找点 \\- 表示查找-
regexp 'feiji?' 表示查找 feiji 或者 feij 最后面的1位可选可不选。
regexp '[[: digit:]]{4}' [: digit:]匹配任意数字,{4}出现4次任意数字
上面与regexp '[0-9][0-9][0-9][0-9]' 作用相同。
低位元字符:^文本的开头 regexp '^[0-9\\.]' 以数字或者.开头
$文本的结尾
[[:词的开头
[[:>:]]词的结尾
2.group
1).作用:把行按 字段 分组
2).语法:group by col1 ,col2 ...coln
3).运用场合: 常见统计场合,如按栏目计算帖子数,统计每个人的平均成绩等。
4).5个统计函数
max:求最大 min:求最小 sun:求总和 avg:求平均 count:求总行数
select max(shop_price) from goods ;(列只能有max一项)
6).查询每个栏目下面最贵的商品
select cat_id, max(shop_price) from goods group by cat_id;
(列位置要是拿cat_id就会把第一次遇到的id拿出来)
7).查询每个栏目下的商品种类
select cat_id,count(*) from goods group by cat_id;
3 . having
1).having 与 where 异同点
having 与 where 类似,可筛选数据where后的表达式怎么写,having就怎么写
where针对表中的列发挥作用,查询数据having针对查询结果中的列发挥作用,筛选数据
2).查询出本店价格比市场价格低多少钱,并且把低200元以上的商品筛选出来。
select good_name,market_prices-shop_prices as sheng from goods having sheng > 200;
3).查询挂科2门以上学生的名字及其平均成绩
select name,sum(fenshu<60) as gk ,avg(fenshu)as pj from stu group by name having gk >= 2;
+------+------+---------+
| name | gk | pj |
+------+------+---------+
| 张三 | 2 | 62.6667 |
| 李四 | 2 | 50.0000 |
+------+------+---------+
4 . order
1).order by 排序功能
按一个或多个字段对查询结果进行排序
2).知识点在应用场合描述
各种排序场合,如取热点新闻,发帖状元等
3).排序
汉字,字母,拼音之类的以哈希表值排序
可以根据字段来排序,默认是升序排列,也可以使用desc来声明降序排列;
5 . limit
1).作用
limit在语句最后,起到限制条目的作用
limit(m,n) m:偏移量,如果不写相当于从头开始取 n:取出条目
6.子查询
1).where型
指把内层查询结果作为外层查询的比较条件。
取出最大的序列号的商品
select good_id,goods_name,from goods where good_id = (select max(goods_id)from goods);
2).from型
把内层查询结果当做临时表,供外层sql再次查询,查询的时候后面必须加一个别名as temp;
挂科2门及2门以上的同学的平均成绩。
select name ,avg(fenshu) from stu where name in (select name from (select name ,count(*)as gk from stu where fenshu < 60 group by name having gk >= 2)as temp) group by name;
3).exists型
把外层的查询结果拿到内层,看内层的查询是否成立
查询有商品的栏目
select cat_id,cat_name from categroy where exists(select * from goods where goods.cat_id = categroy.cat_id);
7 表的联结
1).创建联结
select 表1.列2 , 表2列2, 表2列3 from 表1,表2
where 表1.列1 = 表2.列1;记住了where语句后面要加全限定列名
2).内部联结
select 表1.列2 , 表2列2, 表2列3 from 表1 Inner Join表2
on 表1.列1 = 表2.列1;
3).外部联结
select 表1.列2 , 表2列2, 表2列3 from 表1 outer Join表2
on 表1.列1 = 表2.列1;
-->上面都可以使用带聚集函数的联结 如:
select c1.name,c1.id,count(c2.num) from customers as c1,inner join order as c2
on c1.id = c2.id group by c1.id;
总结:
子查询的结果可以当做一个临时表看待,然后用()括起来用。如果是from后面的话要加 as temp .
where 把表达式拿到行上看是否成立来理解
列:当做变量来理解!