1 1.逻辑运算符的补充
2 between 的用法:(在....之间)
3 select column1,column2,......columnN from 表名
4 where columnX between 第一个值 and 第二个值;
5
6 示例:select name,age from customers where age between 26 and 30;
7
8 SQL中逻辑运算符的优先级列表
9 级别 操作符
10 1 ~(Bitwise NOT)
11 2 * 乘法 /除法 %取模
12 3 + 加号 -减号 &(Bitwise AND) ^(Bitwise Exclusive OR)
13 | (bitwise OR)
14 4 =,>,<,>=,<=,<>,!=,!<,!>比较操作符
15 5 NOT
16 6 AND
17 7 ALL,ANY,BETWEEN,IN,LIKE,OR
18 8 = (赋值)
19 注意:sql中可以使用圆括号()来控制优先级
20
21 update更改:
22 update 表名 set column1=value1,column2=value2,....,columnN=valueN
23 where 条件;
24 示例:
25 update customers set age=29 where name="david";
26 注意
27 update customers set age=29;
28 没有where条件语句,意味着将表中所有行的age改为29.
29
30 delete删除操作:(删除符合条件的一行)
31 delete from 表名 where 条件;
32 示例:
33 delete from customers where id=1;
34 注意:
35 delete from customers;不写where条件语句,结果是把表中的所有记录全部删除
36
37 like语句
38 通配符为%:0,1,或更多
39 通配符为_:单一数字或者单一字符
40 示例:
41 select * from 表名 where 列名 like "XXXX%"
42 select * from 表名 where 列名 like "_XXXX_"
43 思考:
44 select * from customers where salary like "_5__.__";
45 select * from customers where salary like "3%";
46 select * from customers where salary like "1_000%";
47 select * from customers where address like "%zh__";
48
49 limit限制:(选取的是符合列的属性)[limit语句永远放在最后面]
50 select * from 表名 limit n;其中n指返回表中的前n条记录
51 select * from 表名 limit m,n;其中m指记录开始的索引号,第一条记录代码是0,n是指从第m条记录开始,取n条记录(索引也是从0开始)
52 示例:
53 select * from customers limit 3; select name,age(可以是一个也可以是多个) from customers limit 3;
54 select * from customers limit 3,2;
55 order by 排序:
56 ASC 升序
57 DESC 降序
58 select 列名 from 表名 [where 条件] order by 列名 ASC;
59 示例:
60 select name,age from customers order by age ASC;
61 注意;
62 order by 后面出现的列名 不必出现在select语句后
63 练习:
64 把customers表中,城市为深圳的顾客,按年龄升序,工资降序排列,并且只返回结果中的第一条记录
65 select * from customers where address="shenzhen" order by age ASC limit 1;
66 elect * from customers where address="shenzhen" order by salary DESC limit 1;
67 select * from customers where address="shenzhen" order by age ASC,salary desc limit 1;
68 [先按age升序排列如果age一样的情况下在按salary降序排列,没有的表中的顺序就是按age排的]
69
70 select * from customers where address="shenzhen" order by age ASC,salary desc ;(先按age排序在按salary排序)
71
72 Group by 分组:
73 group by 用来与聚合函数(比如 count总数,sum求和,avg平均值,min最小值,max最大值)联合使用,得到一个或多个列的结果集
74 语法:
75 select column1,column2,....columnN,聚合函数(表达式) from 表名 where 条件
76 group by column1,column2,....columnN
77 order by column1,column2,....columnN;
78 注意:******
79 1.group by之后的列必须出现在select语句之中
80 2.group by语句必须在where语句之后,order by语句之前
81 select address,max(salary) from customers group by address;
82 select max(salary) from customers group by address;[这个做法是没有多大意义的只会出现salary的数据不会出现其他数据]
83
84 练习:
85 列出各个城市的平均工资,按照平均工资由小到大的排序,并且只返回前三条记录
86 select address,avg(salary) from customers group by address order by avg(salary)asc limit 3;
87
88 注意:
89 因为聚合函数通过作用与一组数据而只返回一个单个值,因此,在select语句中出现的元素要么为一个聚合函数的输入值,要么为group by语句的参数,否则会出错
90 例如如下语句会报错:
91 select address,avg(salary)[聚合函数的输入值],name from customers group by address;
92
93 where语句后不能直接跟聚合函数,如下例子为错误:
94 select address,avg(salary) from customers where avg(salary)>4000 group by address order by avg(salary)asc limit 3;
95
96 Having语句:(加的是聚合函数)
97 Having语句通常与group by语句联合使用,用来过滤由group by语句返回的记录集
98 Having语句的存在弥补了where关键字不能与聚合函数联合使用的不足
99 语法如下:
100 select column1,column2,....colulmnN,聚合函数(表达式)from 表名 where 条件[判断条件的语句不能加聚合函数,聚合函数只能加在having后面]
101 group by column1,column2,....columnN having 条件1,条件2,....,条件N;
102
103 示例:
104 select address,avg(salary) from customers group by address having avg(salary)>4000;
105 练习:
106 分别针对除了北京市以外的其他城市,列出年龄大于25岁的顾客的最高收入,并且该最高收入不得低于3000,将这些信息按照工资的升序排列
107 select address,max(salary) from customers where age>25 and (not address="beijing") group by address having max(salary)>=3000
108 order by max(salary) asc;
109
110 distinct 不同的
111 与select 一起使用,除去重复项,提前唯一的记录项目
112 select distinct column1,column2,.....columnN from 表名 where 条件
113
114 示例:
115 select distinct age from customers;
116 select count(distinct age) from customers;