mysql基本操作(三)-多表記錄操作和運算符優先級

優先級順序 運算符
1 interval
2 binary , collate
3
4 -(一元減號), ~
5 ^
6 *, / , DIV , % , MOD
7 -,+
8 << , >>
9 &
10 |
11 = , <=> , >= , > , <= , < , <> , != , IS , LIKE , REGEXP , IN
12 BETWEEN , CASE , WHEN , THEN , ELSE
13 NOT
14 && , AND
15 || , OR , XOR
16 :=

多表查詢:

student表

這裏寫圖片描述

class表

這裏寫圖片描述

1 內鏈接查詢:select 字段名列表 from table_name (inner) join table_name2 on table_name.字段名=table_name2.字段;

這裏寫圖片描述

2 重命名:發1 表名 as 重命名; 發2 from 表名 重命名;

這裏寫圖片描述

3 左外連接查詢:select 字段名列表 from 表1 left(right) (outer可省略) join 表2 on 表1.字段=表2.字段;

這裏寫圖片描述

4 通過比較運算符查詢(=,!=,>=,<=,<>(不能用於null),>,<):select 字段名列表 from 表1 where 字段名 = (加any可以查詢多行)(select 字段名 from 表2 where 條件);

這裏寫圖片描述

5 通過in查詢:select 字段名列表 fron 表1 where 字段名 in (select 字段名 from 表2);

這裏寫圖片描述

6 通過exists查詢:select 字段名列表 from 表1 where (not) exists (select 字段名列表 from 表2 where 條件);

這裏寫圖片描述

7 通過all和any查詢:select 字段名列表 from 表1 where 字段名 >= (any)all(select 字段名 from 表2);

這裏寫圖片描述

8 正則表達式查詢:

8.1查詢以字符a開頭或以f結尾的記錄:select 字段名列表 from 表 where 字段名 regexp ‘^a’ or 字段名 regexp ‘f$’;

這裏寫圖片描述

8.2 查詢包含b,c,z字母的任意一個的記錄:select 字段名列表 from 表 where 字段名 regexp ‘[bcz]’;([abd-y]表示a,b,d到y的字符)

這裏寫圖片描述

8.3 匹配指定字符串:select 字段名列表 from 表 where 字段名 regexp ‘afn|fsf’;

這裏寫圖片描述

8.4 “.”代替任意一個字符匹配:select 字段名列表 from 表 where 字段名 regexp ‘a.n’;

這裏寫圖片描述

8.5 *和+匹配多個該符合前的字符:select 字段名列表 from 表 where 字段名 regexp ‘a*n’; * 可表示0個字符 “+”至少表示一個字符

這裏寫圖片描述

8.6 使用{M} {M,N}匹配出現至少M次的,最多N次的記錄:select 字段名列表 from 表 where 字段名 regexp ‘as{1}’;

這裏寫圖片描述

9 將表1數據插入表2:insert into 表1(字段名列表) select 字段名列表 from 表2 where 條件;

10 update 更新記錄:update 表名 set 字段名 = 更新後內容 where 條件;

這裏寫圖片描述

11 刪除數據:delete from 表名 where 條件;

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章