select * from tb12; select id,name from tb12; select id,name from tb12 where id > 10 or name =’xxx’; select id,name as cname from tb12 where id > 10 or name =’xxx’; select name,age,11 from tb12;
select from tb12 where id != 1 select from tb12 where id in (1,5,12); select from tb12 where id not in (1,5,12); select from tb12 where id in (select id from tb11) select * from tb12 where id between 5 and 12;
通配符 通配符的意识替换的意思 %能够替换多个字符 _只能替换一个字符
1
2
select * from tb12 wherenamelike"a%"
select * from tb12 wherenamelike"aa_"
分页
1
2
3
4
select * from tb12 limit 10;
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
后期的Python应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# page = input('请输入要查看的页码')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 第一页1
# select * from tb12 limit 10,10;第二页2
```
- 排序
```sql
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc; # 这是优先级 先按照age倒序,后按照id排序(ID中有相同的)
取后10条数据:先倒序后去取
select * from tb12 order by id desc limit 10;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mysql> select * from t5 order by name desc,id desc;
+----+------+
| id | name |
+----+------+
| 5 | eee |
| 4 | ddd |
| 3 | ccc |
| 2 | aaa |
| 1 | aaa |
+----+------+
5 rows in set (0.00 sec)
mysql> select * from t5 order by name desc,id asc;
+----+------+
| id | name |
+----+------+
| 5 | eee |
| 4 | ddd |
| 3 | ccc |
| 1 | aaa |
| 2 | aaa |
+----+------+
分组
select count(id),max(id),part_id from userinfo5 group by part_id;
- count
- max
- min
- sum
- avg
如果对于聚合函数结果进行二次筛选时?必须使用having ,不能使用where select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
连表操作
连表操作主要是把两张表显示在一张表上,主要用过join
1
2
3
4
5
6
7
8
9
10
select * from userinfo5,department5 -- 这种是笛卡尔积的形式 即所有的乘积
select * from userinfo5,department5 where userinfo5.part_id = department5.id;
左边全部显示
select * from userinfo5 leftjoin department5 on userinfo5.part_id = department5.id
右边全部显示
select * from userinfo5 rightjoin department5 on userinfo5.part_id = department5.id
select * from department5 leftjoin userinfo5 on userinfo5.part_id = department5.id;这种就是变相的right
如果一张表显示全部,但是另一张表还有多的内容的时候,就会出现空null
inner join 将出现null时一行隐藏
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
```
```sql
mysql> select * from t1 leftjoin t5 on t1.id=t5.id;
+----+-------+------+------+
| id | name | id | name |
+----+-------+------+------+
| 1 | aaa | 1 | aaa |
| 2 | aaa | 2 | aaa |
| 3 | ccc | 3 | ccc |
| 4 | ddd | 4 | ddd |
| 5 | eee | 5 | eee |
| 6 | hahah | NULL | NULL |
+----+-------+------+------+
隐藏空行
1
2
3
4
5
6
7
8
9
10
11
mysql> select * from t1 inner join t5 on t1.id=t5.id;