CS-Notes/notes/SQL 语法.md

735 lines
14 KiB
Markdown
Raw Normal View History

2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:54 +08:00
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD>ѯ](#<23><>ѯ)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](<><CDA8><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B4A6>)
* [<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD>Ӳ<EFBFBD>ѯ](#<23>Ӳ<EFBFBD>ѯ)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD>ѯ](#<23><><EFBFBD>ϲ<EFBFBD>ѯ)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
* [ɾ<EFBFBD><EFBFBD>](<><C9BE>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD>޸ı<EFBFBD>](#<23>޸ı<DEB8>)
* [<EFBFBD><EFBFBD>ͼ](#<23><>ͼ)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><EFBFBD><E6B4A2><EFBFBD><EFBFBD>)
* [<EFBFBD>α<EFBFBD>](#<23>α<EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
* [<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>](#<23>ַ<EFBFBD><D6B7><EFBFBD>)
* [Ȩ<EFBFBD>޹<EFBFBD><EFBFBD><EFBFBD>](<>޹<EFBFBD><DEB9><EFBFBD>)
2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ洢<EFBFBD><EFBFBD><EFBFBD>洢ʲô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ηֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD>ͱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ģ<EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
SQL<EFBFBD><EFBFBD>Structured Query Language)<29><><EFBFBD><EFBFBD>׼ SQL <20><> ANSI <20><>׼ίԱ<CEAF><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>Ϊ ANSI SQL<51><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD> DBMS <20><><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD> PL/SQL<51><4C>Transact-SQL <20>ȡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
# <20><>ѯ
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
SQL <20><><EFBFBD><EFBFBD><E4B2BB><EFBFBD>ִ<EFBFBD>Сд<D0A1><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD> DBMS <20>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>á<EFBFBD>
2018-02-20 10:40:05 +08:00
**DISTINCT**
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ֵֻͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>Ρ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT DISTINCT col1, col2
FROM mytable;
```
**LIMIT**
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>ʼ<EFBFBD>У<EFBFBD><EFBFBD><EFBFBD> 0 <20><>ʼ<EFBFBD><CABC><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ 5 <20>е<EFBFBD> SQL<51><4C>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
LIMIT 5;
```
```sql
SELECT *
FROM mytable
LIMIT 0, 5;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD> 3 \~ 5 <20>У<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
LIMIT 2, 3;
```
2018-02-22 14:47:54 +08:00
**ע<><D7A2>**
2018-02-20 10:40:05 +08:00
```sql
2018-02-22 14:47:54 +08:00
# ע<><D7A2>
2018-02-20 10:40:05 +08:00
SELECT *
2018-02-22 14:47:54 +08:00
FROM mytable -- ע<><D7A2>
/* ע<><D7A2>1
ע<><D7A2>2 */
2018-02-20 10:40:05 +08:00
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**ASC**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϣ<EFBFBD>
**DESC**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
ORDER BY col1 DESC, col2 ASC;
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ӧ<EFBFBD>ò<EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD>ڷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD><EFBFBD>й<EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷdz<EFBFBD><EFBFBD>󣬵<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺ܶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD>˷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
WHERE col IS NULL;
```
2018-02-22 14:47:54 +08:00
<EFBFBD>±<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD> WHERE <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD>õIJ<C3B5><C4B2><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
| <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> | ˵<><CBB5> |
2018-02-20 10:40:05 +08:00
| ------------ | ------------ |
2018-02-22 14:47:54 +08:00
| = < > | <20><><EFBFBD><EFBFBD> С<><D0A1> <20><><EFBFBD><EFBFBD> |
| <> != | <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> |
| <= !> | С<>ڵ<EFBFBD><DAB5><EFBFBD> |
| >= !< | <20><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD> |
| BETWEEN | <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ֮<D6B5><D6AE> |
| IS NULL | ΪNULLֵ |
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
Ӧ<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD>NULL <20><> 0 <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**AND OR** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD> AND<4E><44><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD><CBB1><EFBFBD>ʽ<EFBFBD><EFBFBD><E6BCB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> AND <20><> OR ʱ<><CAB1>Ӧ<EFBFBD><D3A6>ʹ<EFBFBD><CAB9> () <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**IN** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>һ<EFBFBD><D2BB>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>Խ<EFBFBD>һ<EFBFBD><D2BB> SELECT <20>Ӿ䣬<D3BE>Ӷ<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>һ<EFBFBD><D2BB>ֵ<EFBFBD><D6B5>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**NOT** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
# ͨ<><CDA8><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD>ֶΡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
- **%** ƥ<><C6A5> >=0 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \*<2A><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
- **\_** ƥ<><C6A5> ==1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \.<2E><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
- **[ ]** <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><E4BCAF><EFBFBD>ڵ<EFBFBD><DAB5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD> ^ <20><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD> Like <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ƥ<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
2018-02-22 14:47:54 +08:00
WHERE col LIKE '[^AB]%' -- <20><><EFBFBD><EFBFBD>AB<41><42>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2018-02-20 10:40:05 +08:00
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ڿ<EFBFBD>ͷ<EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dz<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȿͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD><EFBFBD>ö࣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٵĻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʹ<EFBFBD><EFBFBD> **AS** <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
2018-02-20 10:40:05 +08:00
```sql
SELECT col1*col2 AS alias
FROM mytable
```
2018-02-22 14:47:54 +08:00
**Concat()** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶΡ<D6B6><CEA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>ʹ<EFBFBD>ÿո<C3BF><D5B8><EFBFBD>һ<EFBFBD><D2BB>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ϊ<EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵĽ<D3B5><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ŀո<C4BF><D5B8><EFBFBD>ʹ<EFBFBD><CAB9> **TRIM()** <20><><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>β<EFBFBD>ո<EFBFBD><D5B8><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT Concat(TRIM(col1), ' (', TRIM(col2), ')')
FROM mytable
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> DBMS <20>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD>ͬ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
## <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
| <20><><EFBFBD><EFBFBD> | ˵<><CBB5> |
2018-02-20 10:40:05 +08:00
| ------------ | ------------ |
2018-02-22 14:47:54 +08:00
| LEFT() RIGHT() | <20><><EFBFBD>߻<EFBFBD><DFBB><EFBFBD><EFBFBD>ұߵ<D2B1><DFB5>ַ<EFBFBD> |
| LOWER() UPPER() | ת<><D7AA>ΪСд<D0A1><D0B4><EFBFBD>ߴ<EFBFBD>д |
| LTRIM() RTIM() | ȥ<><C8A5><EFBFBD><EFBFBD><EFBFBD>߻<EFBFBD><DFBB><EFBFBD><EFBFBD>ұߵĿո<C4BF> |
| LENGTH() | <20><><EFBFBD><EFBFBD> |
| SUNDEX() | ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ֵ |
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>**SOUNDEX()** <20>ǽ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><C7B8>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>Ƚϡ<C8BD>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
WHERE SOUNDEX(col1) = SOUNDEX('apple')
```
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B4A6>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD>ʽ<EFBFBD><EFBFBD>YYYY-MM-DD
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>HH:MM:SS
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
|<7C><> <20><> | ˵ <20><>|
2018-02-20 10:40:05 +08:00
| --- | --- |
2018-02-22 14:47:54 +08:00
| AddDate() | <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ܵȣ<DCB5>|
| AddTime() | <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD>䣨ʱ<E4A3A8><CAB1><EFBFBD>ֵȣ<D6B5>|
| CurDate() | <20><><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD> |
| CurTime() | <20><><EFBFBD>ص<EFBFBD>ǰʱ<C7B0><CAB1> |
|Date() |<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>|
|DateDiff() |<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE>|
|Date_Add() |<7C>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BAAF>|
|Date_Format() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>ʱ<EFBFBD>䴮|
|Day()| <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|
|DayOfWeek() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD>|
|Hour() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Сʱ<D0A1><CAB1><EFBFBD><EFBFBD>|
|Minute() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD>ķ<EFBFBD><C4B7>Ӳ<EFBFBD><D3B2><EFBFBD>|
|Month() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>·ݲ<C2B7><DDB2><EFBFBD>|
|Now() |<7C><><EFBFBD>ص<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ں<EFBFBD>ʱ<EFBFBD><CAB1>|
|Second() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>벿<EFBFBD><EBB2BF>|
|Time() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD>䲿<EFBFBD><E4B2BF>|
|Year() |<7C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD>|
2018-02-20 10:40:05 +08:00
```sql
mysql> SELECT NOW();
-> '2017-06-28 14:01:52'
```
2018-02-22 14:47:54 +08:00
## <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
| <20><><EFBFBD><EFBFBD> | ˵<><CBB5> |
2018-02-20 10:40:05 +08:00
| --- | --- |
2018-02-22 14:47:54 +08:00
| SIN() | <20><><EFBFBD><EFBFBD> |
|COS() | <20><><EFBFBD><EFBFBD> |
| TAN() | <20><><EFBFBD><EFBFBD> |
| ABS() | <20><><EFBFBD><EFBFBD>ֵ |
| SQRT() | ƽ<><C6BD><EFBFBD><EFBFBD>|
| MOD() | <20><><EFBFBD><EFBFBD>|
| EXP() | ָ<><D6B8>|
| PI() | Բ<><D4B2><EFBFBD><EFBFBD>|
|RAND() | <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|
## <20><><EFBFBD><EFBFBD>
|<7C><> <20><><20><>|
2018-02-20 10:40:05 +08:00
| --- | --- |
2018-02-22 14:47:54 +08:00
|AVG() |<7C><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD>ƽ<EFBFBD><C6BD>ֵ|
|COUNT()| <20><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>|
|MAX()| <20><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ֵ|
|MIN()| <20><><EFBFBD><EFBFBD>ij<EFBFBD>е<EFBFBD><D0B5><EFBFBD>Сֵ|
|SUM() |<7C><><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ֵ֮<D6B5><D6AE>|
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
AVG() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL <20>С<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
DISTINCT <20>ؼ<EFBFBD><D8BC>ֻ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ܲ<EFBFBD>ͬ<EFBFBD><CDAC>ֵ<EFBFBD><D6B5>
2018-02-20 10:40:05 +08:00
```sql
SELECT AVG(DISTINCT col1) AS avg_col
FROM mytable
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><EFBFBD><EFBFBD>ͬһ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>û<EFBFBD><EFBFBD>ܺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><EFBFBD>ֵ<EFBFBD>ȡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> col <20><><EFBFBD>򲢷<EFBFBD><F2B2A2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT col, COUNT(*) AS num
FROM mytable
GROUP BY col;
```
2018-02-22 14:47:54 +08:00
WHERE <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>HAVING <20><><EFBFBD>˷<EFBFBD><CBB7><EFBFBD>й<EFBFBD><D0B9><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT col, COUNT(*) AS num
FROM mytable
WHERE col > 2
GROUP BY col
HAVING COUNT(*) >= 2;
```
2018-02-22 14:47:54 +08:00
GROUP BY <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ֶΣ<D6B6><CEA3><EFBFBD> ORDER BY Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD>Ծۼ<D4BE><DBBC>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT col, COUNT(*) AS num
FROM mytable
GROUP BY col
ORDER BY num;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
1. GROUP BY <20>Ӿ<EFBFBD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> WHERE <20>Ӿ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>ORDER BY <20>Ӿ<EFBFBD>֮ǰ<D6AE><C7B0>
2. <20><><EFBFBD>˻<EFBFBD><CBBB>ܼ<EFBFBD><DCBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD>⣬SELECT <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ÿһ<C3BF>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> GROUP BY <20>Ӿ<EFBFBD><D3BE>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
3. NULL <20><><EFBFBD>лᵥ<D0BB><E1B5A5><EFBFBD><EFBFBD>Ϊһ<CEAA>
4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SQL ʵ<>ֲ<EFBFBD>֧<EFBFBD><D6A7> GROUP BY <20>о<EFBFBD><D0BE>пɱ䳤<C9B1>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
# <20>Ӳ<EFBFBD>ѯ
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD><EFBFBD>ֻ<EFBFBD>ܷ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ WHRER <20><><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```
SELECT *
FROM mytable1
WHERE col1 IN (SELECT col2
FROM mytable2);
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD>ִ<EFBFBD><EFBFBD>һ<EFBFBD>Σ<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT cust_name, (SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id)
AS orders_num
FROM Customers
ORDER BY cust_name;
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> JOIN <20>ؼ<EFBFBD><D8BC>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> ON<4F><4E>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> AS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶκͱ<CEBA><CDB1><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˼<EFBFBD><CBBC><EFBFBD> SQL <20><><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳƵ<EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>ʹ<EFBFBD><EFBFBD> INNER JOIN <20>ؼ<EFBFBD><D8BC>֡<EFBFBD>
2018-02-20 10:40:05 +08:00
```
select a, b, c
from A inner join B
on A.key = B.key
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><EFBFBD><EFBFBD>ȷʹ<EFBFBD><EFBFBD> INNER JOIN<49><4E><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD> WHERE <20>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>õ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```
select a, b, c
from A, B
where A.key = B.key
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>صѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>һ<EFBFBD>֣<EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
һ<EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>Ҫ<EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD> Jim <20><><EFBFBD><EFBFBD>ͬһ<CDAC><D2BB><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A>Ӳ<EFBFBD>ѯ<EFBFBD>汾**
2018-02-20 10:40:05 +08:00
```
select name
from employee
where department = (
select department
from employee
where name = "Jim");
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>Ӱ汾**
2018-02-20 10:40:05 +08:00
```
select name
from employee as e1, employee as e2
where e1.department = e2.department
and e1.name = "Jim";
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD>ѯ<EFBFBD><EFBFBD>Ч<EFBFBD>ʸߡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
## <20><>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
2018-02-20 10:40:05 +08:00
```
select *
from employee natural join department;
```
2018-02-22 14:47:54 +08:00
## <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӿ<EFBFBD><EFBFBD>DZ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й˿͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ĺ˿͡<EFBFBD>
2018-02-20 10:40:05 +08:00
```
select Customers.cust_id, Orders.order_num
from Customers left outer join Orders
on Customers.cust_id = Orders.curt_id
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫͳ<EFBFBD>ƹ˿͵Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>þۼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```
select Customers.cust_id,
COUNT(Orders.order_num) as num_ord
from Customers left outer join Orders
on Customers.cust_id = Orders.curt_id
group by Customers.cust_id
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD>ϲ<EFBFBD>ѯ
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD> **UNION** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>߾ۼ<DFBE><DBBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
Ĭ<EFBFBD>ϻ<EFBFBD>ȥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD>У<EFBFBD>ʹ<EFBFBD><EFBFBD> UNION ALL <20><>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ֻ<EFBFBD>ܰ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> ORDER BY <20>Ӿ䣬<D3BE><E4A3AC><EFBFBD>ұ<EFBFBD><D2B1><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT col
FROM mytable
WHERE col = 1
UNION
SELECT col
FROM mytable
WHERE col =2;
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
INSERT INTO mytable(col1, col2)
VALUES(val1, val2);
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
INSERT INTO mytable1(col1, col2)
SELECT col1, col2
FROM mytable2;
```
2018-02-22 14:47:54 +08:00
**<2A><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>Ƶ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>±<EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
CREATE TABLE newtable AS
SELECT * FROM mytable;
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
UPDATE mytable
SET col = val
WHERE id = 1;
```
2018-02-22 14:47:54 +08:00
# ɾ<><C9BE>
2018-02-20 10:40:05 +08:00
```sql
DELETE FROM mytable
WHERE id = 1;
```
2018-02-22 14:47:54 +08:00
**TRUNCATE TABLE** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ձ<EFBFBD><D5B1><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD>ø<EFBFBD><EFBFBD>º<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱһ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD> WHERE <20>Ӿ䣬<D3BE><E4A3AC>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SELECT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2>ԣ<EFBFBD><D4A3><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
CREATE TABLE mytable (
id INT NOT NULL AUTO_INCREMENT,
col1 INT NOT NULL DEFAULT 1,
col2 VARCHAR(45) NULL,
col3 DATE NULL,
PRIMARY KEY (`id`));
```
2018-02-22 14:47:54 +08:00
# <20>޸ı<DEB8>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
ALTER TABLE mytable
ADD col CHAR(20);
```
2018-02-22 14:47:54 +08:00
**ɾ<><C9BE><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
ALTER TABLE mytable
DROP COLUMN col;
```
2018-02-22 14:47:54 +08:00
**ɾ<><C9BE><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
DROP TABLE mytable;
```
2018-02-22 14:47:54 +08:00
# <20><>ͼ
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>Ҳ<EFBFBD>Ͳ<EFBFBD><EFBFBD>ܶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ºô<EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
1. <20>򻯸<EFBFBD><F2BBAFB8>ӵ<EFBFBD> SQL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>
2. ֻʹ<D6BB><CAB9>ʵ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
3. ͨ<><CDA8>ֻ<EFBFBD><D6BB><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>Ȩ<EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD>֤<EFBFBD><D6A4><EFBFBD>ݵİ<DDB5>ȫ<EFBFBD>ԣ<EFBFBD>
4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>ʽ<EFBFBD>ͱ<EFBFBD>ʾ<EFBFBD><CABE>
2018-02-20 10:40:05 +08:00
```sql
CREATE VIEW myview AS
SELECT Concat(col1, col2) AS concat_col, col3*col4 AS count_col
FROM mytable
WHERE col5 = val;
```
2018-02-22 14:47:54 +08:00
# <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>̿<EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>һϵ<EFBFBD><EFBFBD> SQL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**ʹ<>ô洢<C3B4><E6B4A2><EFBFBD>̵ĺô<C4BA>**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
1. <20><>ʵ<EFBFBD>ַ<EFBFBD>װ<EFBFBD><D7B0><EFBFBD>˴<CBB4><E6B4A2><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򵥣<EFBFBD>Ҳ<EFBFBD><D2B2>֤<EFBFBD>˰<EFBFBD>ȫ<EFBFBD>ԣ<EFBFBD>
2. <20><><EFBFBD>Ը<EFBFBD><D4B8>ô<EFBFBD><C3B4>
3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EBA3AC><EFBFBD>˾<EFBFBD><CBBE>кܸߵ<DCB8><DFB5><EFBFBD><EFBFBD>ܡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ; Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>˷ֺţ<D6BA><C5A3><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD>ַֺŵ<D6BA><C5B5><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFB7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> in<69><6E>out <20><> inout <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD> select into <20><><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ÿ<EFBFBD><EFBFBD>ֻ<EFBFBD>ܸ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD>ּ<EFBFBD><EFBFBD>ϵIJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
delimiter //
create procedure myprocedure( out ret int )
begin
declare y int;
select sum(col1)
from mytable
into y;
select y*y into ret;
end //
delimiter ;
```
```sql
call myprocedure(@ret);
select @ret;
```
2018-02-22 14:47:54 +08:00
# <20>α<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>ڴ洢<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>α<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>α<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD>ʽӦ<EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ġ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**ʹ<><CAB9><EFBFBD>α<EFBFBD><CEB1><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>裺**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
1. <20><><EFBFBD><EFBFBD><EFBFBD>α<CEB1><EAA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ʵ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
2. <20><><EFBFBD><EFBFBD><EFBFBD>α
3. ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
4. <20>ر<EFBFBD><D8B1>α
2018-02-20 10:40:05 +08:00
```sql
delimiter //
create procedure myprocedure(out ret int)
begin
declare done boolean default 0;
declare mycursor cursor for
select col1 from mytable;
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>continue handler<65><72><EFBFBD><EFBFBD> sqlstate '02000' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4> set done = 1
2018-02-20 10:40:05 +08:00
declare continue handler for sqlstate '02000' set done = 1;
open mycursor;
repeat
fetch mycursor into ret;
select ret;
until done end repeat;
close mycursor;
end //
delimiter ;
```
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ִ<EFBFBD>У<EFBFBD>DELETE<EFBFBD><EFBFBD>INSERT<EFBFBD><EFBFBD>UPDATE
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>֮ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ִ<EFBFBD>У<EFBFBD>֮ǰִ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> BEFORE <20>ؼ<EFBFBD><D8BC>֣<EFBFBD>֮<EFBFBD><D6AE>ִ<EFBFBD><D6B4>ʹ<EFBFBD><CAB9> AFTER <20>ؼ<EFBFBD><D8BC>֡<EFBFBD>BEFORE <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>;<EFBFBD><CDBE><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
INSERT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϊ NEW <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
CREATE TRIGGER mytrigger AFTER INSERT ON mytable
FOR EACH ROW SELECT NEW.col;
```
2018-02-22 14:47:54 +08:00
DELETE <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϊ OLD <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ġ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
UPDATE <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϊ NEW <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϊ OLD <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NEW <20>ǿ<EFBFBD><C7BF>Ա<EFBFBD><D4B1>޸ĵأ<C4B5><D8A3><EFBFBD> OLD <20><>ֻ<EFBFBD><D6BB><EFBFBD>ġ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƹ<EFBFBD><EFBFBD>٣<EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸ļ<EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ű<EFBFBD><EFBFBD>С<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
MySQL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> CALL <20><><EFBFBD><EFBFBD> <20><>Ҳ<EFBFBD><D2B2><EFBFBD>Dz<EFBFBD><C7B2>ܵ<EFBFBD><DCB5>ô洢<C3B4><E6B4A2><EFBFBD>̡<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>transaction<6F><6E>ָһ<D6B8><D2BB> SQL <20><><EFBFBD>
2. <20><><EFBFBD>ˣ<EFBFBD>rollback<63><6B>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> SQL <20><><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9>̣<EFBFBD>
3. <20><EFBFBD><E1BDBB>commit<69><74>ָ<EFBFBD><D6B8>δ<EFBFBD><EFBFBD><E6B4A2> SQL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
4. <20><><EFBFBD><EFBFBD><EFBFBD>㣨savepoint<6E><74>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>ʱռλ<D5BC><CEBB><EFBFBD><EFBFBD>placeholder<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><EFBFBD><EFBFBD> SELECT <20><><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD> SELECT <20><><EFBFBD><EFBFBD>Ҳû<D2B2><C3BB><EFBFBD>壻Ҳ<E5A3BB><D2B2><EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD> CRETE <20><> DROP <20><><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
MySQL <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ύĬ<E1BDBB><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><E1BDBB>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ÿִ<C3BF><D6B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB>һ<E1BDBB>Ρ<EFBFBD><CEA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> START TRANSACTION <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>ʽ<EFBFBD><EFBFBD><E1BDBB><EFBFBD><EFBFBD> COMMIT <20><> ROLLBACK <20><><EFBFBD><EFBFBD>ִ<EFBFBD>к<EFBFBD><D0BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>رգ<D8B1><D5A3><EFBFBD><EFBFBD>»ָ<C2BB><D6B8><EFBFBD>ʽ<EFBFBD><EFBFBD><E1BDBB>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> autocommit Ϊ 0 <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><E1BDBB>ֱ<EFBFBD><D6B1> autocommit <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1 <20>Ż<EFBFBD><C5BB><EFBFBD><E1BDBB>autocommit <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ġ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>㣬ROLLBACK <20><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD> START TRANSACTION <20><><EFBFBD><EFBFBD><E4B4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD><CBB1><EFBFBD><EFBFBD><EFBFBD><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ROLLBACK <20><>ָ<EFBFBD><D6B8><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
START TRANSACTION
// ...
SAVEPOINT delete1
// ...
ROLLBACK TO delete1
// ...
COMMIT
```
2018-02-22 14:47:54 +08:00
# <20>ַ<EFBFBD><D6B7><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
1. <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ<EFBFBD><CEAA>ĸ<EFBFBD>ͷ<EFBFBD><CDB7>ŵļ<C5B5><C4BC>ϣ<EFBFBD>
2. <20><><EFBFBD><EFBFBD>Ϊij<CEAA><C4B3><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>ʾ<EFBFBD><CABE>
3. У<><D0A3><EFBFBD>ַ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>αȽϣ<C8BD><CFA3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD>⣬Ҳ<EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
CREATE TABLE mytable
(col VARCHAR(10) CHARACTER SET latin COLLATE latin1_general_ci )
DEFAULT CHARACTER SET hebrew COLLATE hebrew_general_ci;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򡢷<EFBFBD><EFBFBD><EFBFBD>ʱָ<EFBFBD><EFBFBD>У<EFBFBD>ԣ<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SELECT *
FROM mytable
ORDER BY col COLLATE latin1_general_ci;
```
2018-02-22 14:47:54 +08:00
# Ȩ<>޹<EFBFBD><DEB9><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
MySQL <20><><EFBFBD>˻<EFBFBD><CBBB><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> mysql <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>С<EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
USE mysql;
SELECT user FROM user;
```
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
CREATE USER myuser IDENTIFIED BY 'mypassword';
```
2018-02-22 14:47:54 +08:00
<EFBFBD>´<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD>Ȩ<EFBFBD>ޡ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A>޸<EFBFBD><DEB8>˻<EFBFBD><CBBB><EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
RENAME myuser TO newuser;
```
2018-02-22 14:47:54 +08:00
**ɾ<><C9BE><EFBFBD>˻<EFBFBD>**
2018-02-20 10:40:05 +08:00
```sql
DROP USER myuser;
```
2018-02-22 14:47:54 +08:00
**<2A>鿴Ȩ<E9BFB4><C8A8>**
2018-02-20 10:40:05 +08:00
```sql
SHOW GRANTS FOR myuser;
```
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c73aa08e-a987-43c9-92be-adea4a884c25.png)
2018-02-22 14:47:54 +08:00
<EFBFBD>˻<EFBFBD><EFBFBD><EFBFBD> username@host <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>壬username@% ʹ<>õ<EFBFBD><C3B5><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD>Ȩ<EFBFBD><C8A8>**
2018-02-20 10:40:05 +08:00
```sql
GRANT SELECT, INSERT ON mydatabase.* TO myuser;
```
2018-02-22 14:47:54 +08:00
**ɾ<><C9BE>Ȩ<EFBFBD><C8A8>**
2018-02-20 10:40:05 +08:00
```sql
REVOKE SELECT, INSERT ON mydatabase.* FROM myuser;
```
2018-02-22 14:47:54 +08:00
GRANT <20><> REVOKE <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD><CFBF>Ʒ<EFBFBD><C6B7><EFBFBD>Ȩ<EFBFBD>ޣ<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> GRANT ALL<4C><4C> REVOKE ALL<4C><4C>
- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ⣬ʹ<E2A3AC><CAB9> ON database.\*<2A><>
- <20>ض<EFBFBD><D8B6>ı<EFBFBD><C4B1><EFBFBD>ʹ<EFBFBD><CAB9> ON database.table<6C><65>
- <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD>У<EFBFBD>
- <20>ض<EFBFBD><D8B6>Ĵ洢<C4B4><E6B4A2><EFBFBD>̡<EFBFBD>
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> Password() <20><><EFBFBD><EFBFBD>
2018-02-20 10:40:05 +08:00
```sql
SET PASSWROD FOR myuser = Password('newpassword');
```