文章目錄
數據庫的基礎操作
mysql採用的同樣是sql語法,mysql數據表的常用操作已經總結在這篇文章
創建新的數據庫
創建數據庫
create database 數據庫名 (字符集)
mysql> create database test1 character set utf8 collate utf8_bin;
查看數據庫的字符集
show create database 數據庫名
mysql> show create database test1
-> ;
+----------+---------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------+
| test1 | CREATE DATABASE `test1` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ |
+----------+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看所有的數據庫
show databases;
刪除數據庫
drop database 數據庫名
使用數據庫
use database 數據庫名字;
查看數據庫裏的所有表
show tables;
數據表的基礎操作
創建表
mysql> create table classes
-> (
-> id int(11) primary key,
-> name varchar(22),
-> age int(11),
-> salary float
-> );
查看錶結構
mysql> desc classes;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
表中添加新字段
## alter table 表名 add 列名 類型
mysql> alter table classes add location varchar(50);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc classes;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
字段重命名
## alert table 表名 change 原名 新名 類型及約束
mysql> alter table classes change location LOCATION varchar(50);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc classes;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
| LOCATION | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
刪除字段
alter table 表名 drop 列名
mysql> alter table test3 drop id;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
刪除表
drop table 表名
更新數據
語法:
UPDATE table_name SET column1=value1,column2=value2,...WHERE some_column=some_value;
mysql> update classes set name='yu',salary=7200 where id=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from classes;
+----+-------+------+--------+----------+
| id | name | age | salary | LOCATION |
+----+-------+------+--------+----------+
| 1 | zhou | 26 | 12000 | aaa |
| 2 | wu | 22 | 9200 | bbb |
| 3 | zheng | 20 | 8500 | ccc |
| 4 | li | 18 | 13850 | ddd |
| 5 | yu | 20 | 7200 | NULL |
| 6 | cao | NULL | NULL | NULL |
+----+-------+------+--------+----------+
6 rows in set (0.00 sec)
插入數據
全列插入:值的順序與表結構字段的順序完全一一對應
insert into 表名 values (...)
mysql> insert into class2 values(001,"zhao",1,488);
部分列插入:值的順序與給出的列順序對應
insert into 表名 (列1,列2..) values(值1,值2..)
mysql> insert into class(id,name) values(3,"sun");
全列多行插入
insert into 表名 values(...),(...)...;
mysql> insert into class values(4,"cao",20,"aaa",8500),(5,"wei",18,"bbb",)7000),(6,"yang",21,"ccc",11000);
部分列多行插入
insert into 表名(列1,...) values(值1,...),(值1,...)...;
mysql> insert into class(id,name) values(10,"wang"),(11,"wu"),(12,"liu");
主鍵約束
主鍵約束要求主鍵列的數據唯一,並且不允許爲空
語法: 字段名 數據類型 PRIMARY KEY [默認值]
-> id INT(11) PRIMARY KEY,
或
在定義完所有列之後指定主鍵
-> PRIMARY KEY(id)
多字段聯合主鍵
## PRIMARY KEY [字段1,字段2,....]
mysql> create table test2
-> (
-> name varchar(25),
-> de int(11),
-> salary float,
-> primary key(name,de)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc test2;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(25) | NO | PRI | NULL | |
| de | int(11) | NO | PRI | NULL | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
外鍵約束
外鍵用來在兩個表數據
之間建立連接,它可以是一列或者多列
目的是一張表insert
數據時要受到另一個表的約束
## [CONSTRAINT<外鍵名>] FOREIGN KEY [字段名1,字段名2...] REFERENCES<主表名> 主鍵列1[主鍵列
2...]
# 創建新表與class做約束
mysql> create table class2
-> (
-> id int primary key,
-> name varchar(22),
-> deptID int,
-> score float,
-> constraint ID foreign key(deptID) references class(id)
-> );
Query OK, 0 rows affected (0.01 sec)
#class表插入數據
mysql> select * from class;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 1 | zhao | NULL | NULL | NULL |
| 2 | qian | NULL | NULL | NULL |
| 3 | sun | NULL | NULL | NULL |
+----+------+------+----------+--------+
3 rows in set (0.00 sec)
# class2 再插入數據,class表id不存在的class2不能再寫入數據
mysql> insert into class2 values(001,"zhao",1,488);
Query OK, 1 row affected (0.00 sec)
mysql> insert into class2 values(002,"qian",4,375);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test1`.`class2`, CONSTRAINT `ID` FOREIGN KEY (`deptID`) REFERENCES `class` (`id`))
若表已經創建好,需要設置外鍵
添加約束
alter table class2 add foreign key ID(deptID) references class(id);
刪除外鍵約束
alter table class2 drop foreign key ID;
非空約束
非空約束指字段的值不能爲空
not null
是指該字段不能爲空,但是可以重複;
unique
是值該字段不可以重複,但是可以爲空;
字段名 數據類型 not null
-> name VARCHAR(25) NOT NULL,
[CONSTRATIN <約束名>] UNIQUE (<字段名>)
-> CONSTRAINT STH UNIQUE(name)
## 後期添加約束
mysql> alter table test3 modify deptID int(25) unique;
其中modify
的意思可以理解爲重構,你甚至可以用modify來進行對其他約束的創建
例如下面:
alter table test modify deptID char(10) not null;
建立非空約束
alter table test modify deptID char(10);
刪除非空約束
默認約束
默認約束指定某列的默認值。
字段名 數據類型 DEFAULT 默認值
mysql> create table test3
-> (
-> id int primary key,
-> name varchar(25) not null,
-> deptID int(25) default 0001,
-> salary float
-> );
mysql> desc test3
-> ;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(25) | NO | | NULL | |
| deptID | int(25) | YES | | 1 | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
表的屬性值自動增加
## 字段名 數據類型 AUTO_INCREMENT
mysql> create table class2
-> (
-> id INT(11) PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(25) NOT NULL,
-> deptId INT(11),
-> salary FLOAT
-> );
Query OK, 0 rows affected (0.00 sec)
#若表已經存在
mysql> alter table class2 modify id int(11) auto_increment;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
在表裏面插入數據,不寫入 id
mysql> insert into class2 (name,score) values('li',96),('liu',63);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from class2
-> ;
+----+------+--------+-------+
| id | name | deptID | score |
+----+------+--------+-------+
| 1 | zhao | 1 | 488 |
| 2 | li | NULL | 96 |
| 3 | liu | NULL | 63 |
+----+------+--------+-------+
3 rows in set (0.00 sec)
更改表的存儲引擎
## ALTER TABLE <表名> ENGINE=<更改後的存儲引擎>
mysql> alter table classes engine=MyISAM;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table classes\G;
*************************** 1. row ***************************
Table: classes
Create Table: CREATE TABLE `classes` (
`id` int(11) NOT NULL,
`name` varchar(22) COLLATE utf8_bin DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL,
`LOCATION` varchar(50) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.00 sec)
數據表的查詢
基本查詢
查詢所有列
## select * from 表名
mysql> select * from class;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 1 | zhao | NULL | NULL | NULL |
| 2 | qian | NULL | NULL | NULL |
| 3 | sun | NULL | NULL | NULL |
| 4 | cao | 20 | aaa | 8500 |
| 5 | wei | 18 | bbb | 7000 |
| 6 | yang | 21 | ccc | 11000 |
| 10 | wang | NULL | NULL | NULL |
| 11 | wu | NULL | NULL | NULL |
| 12 | liu | NULL | NULL | NULL |
+----+------+------+----------+--------+
9 rows in set (0.01 sec)
查詢指定列
## select 列1,列2,...from 表名;
mysql> select id,name from class;
+----+------+
| id | name |
+----+------+
| 1 | zhao |
| 2 | qian |
| 3 | sun |
| 4 | cao |
| 5 | wei |
| 6 | yang |
| 10 | wang |
| 11 | wu |
| 12 | liu |
+----+------+
9 rows in set (0.00 sec)
條件語句查詢
where後面支持多種運算符,進行條件的處理 ;
常用的運算符如下:
比較運算符 邏輯運算符 模糊查詢 範圍查詢 空判斷
比較運算符如下:
查詢class表id大於等於10的人
mysql> select * from class where id >= 10;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 10 | wang | NULL | NULL | NULL |
| 11 | wu | NULL | NULL | NULL |
| 12 | liu | NULL | NULL | NULL |
+----+------+------+----------+--------+
3 rows in set (0.00 sec)
查詢class表name是“sun”的人
mysql> select * from class where name="sun";
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 3 | sun | NULL | NULL | NULL |
+----+------+------+----------+--------+
1 row in set (0.00 sec)
邏輯運算符
and 與
or 或
not 否
查詢名字是“sun”,並且id小於10
mysql> select * from class where name="sun" and id < 10;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 3 | sun | NULL | NULL | NULL |
+----+------+------+----------+--------+
1 row in set (0.00 sec)
查詢名字是“cao”或年齡是18
mysql> select * from class where name="cao" or age=18;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 4 | cao | 20 | aaa | 8500 |
| 5 | wei | 18 | bbb | 7000 |
+----+------+------+----------+--------+
2 rows in set (0.00 sec)
模糊查詢
like
%表示任意多個任意字符
_表示一個任意字符
查詢名字類似‘ya%’ 或者類似‘li%’
mysql> select * from class where name like 'ya%' or name like 'li%';
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 6 | yang | 21 | ccc | 11000 |
| 12 | liu | NULL | NULL | NULL |
+----+------+------+----------+--------+
2 rows in set (0.00 sec)
範圍查詢
in表示在一個非連續的範圍內
between … and …表示在一個連續的範圍內
查詢class表id爲1 3 5的人
mysql> select * from class where id in(1,3,5);
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 1 | zhao | NULL | NULL | NULL |
| 3 | sun | NULL | NULL | NULL |
| 5 | wei | 18 | bbb | 7000 |
+----+------+------+----------+--------+
3 rows in set (0.00 sec)
查詢
mysql> select * from class where id between 3 and 5;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 3 | sun | NULL | NULL | NULL |
| 4 | cao | 20 | aaa | 8500 |
| 5 | wei | 18 | bbb | 7000 |
+----+------+------+----------+--------+
3 rows in set (0.00 sec)
空判斷
查詢出來條件裏的null的項
mysql> select * from class where age is null;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 1 | zhao | NULL | NULL | NULL |
| 2 | qian | NULL | NULL | NULL |
| 3 | sun | NULL | NULL | NULL |
| 10 | wang | NULL | NULL | NULL |
| 11 | wu | NULL | NULL | NULL |
| 12 | liu | NULL | NULL | NULL |
+----+------+------+----------+--------+
6 rows in set (0.00 sec)
也可以反過來查詢吶
mysql> select * from class where age is not null;
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 4 | cao | 20 | aaa | 8500 |
| 5 | wei | 18 | bbb | 7000 |
| 6 | yang | 21 | ccc | 11000 |
+----+------+------+----------+--------+
3 rows in set (0.00 sec)
查詢排序
將行數據按照列1進行排序,如果某些行 列1 的值相同時,則按照 列2 排序,以此類推
asc從小到大排列,即升序
desc從大到小排序,即降序
默認按照列值從小到大排列(即as升序)
語法使用
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
升序
mysql> select * from classes order by age asc;
+----+-------+------+--------+----------+
| id | name | age | salary | LOCATION |
+----+-------+------+--------+----------+
| 6 | cao | NULL | NULL | NULL |
| 4 | li | 18 | 13850 | ddd |
| 3 | zheng | 20 | 8500 | ccc |
| 5 | yu | 20 | 7200 | NULL |
| 2 | wu | 22 | 9200 | bbb |
| 1 | zhou | 26 | 12000 | aaa |
+----+-------+------+--------+----------+
6 rows in set (0.00 sec)
降序
mysql> select * from classes order by salary desc;
+----+-------+------+--------+----------+
| id | name | age | salary | LOCATION |
+----+-------+------+--------+----------+
| 4 | li | 18 | 13850 | ddd |
| 1 | zhou | 26 | 12000 | aaa |
| 2 | wu | 22 | 9200 | bbb |
| 3 | zheng | 20 | 8500 | ccc |
| 5 | yu | 20 | 7200 | NULL |
| 6 | cao | NULL | NULL | NULL |
+----+-------+------+--------+----------+
6 rows in set (0.00 sec)
limit限制查詢
使用limit限制查詢結果的數量 select返回所有匹配的行,有可能是表中所有的行,如僅僅需要返回第一行或者前幾行,使用limit關鍵字,語法格式如下:
limit [位置偏移量] 行數
從第5行開始,並且只查詢1條記錄
mysql> select * from classes limit 5,1;
+----+------+------+--------+----------+
| id | name | age | salary | LOCATION |
+----+------+------+--------+----------+
| 6 | cao | NULL | NULL | NULL |
+----+------+------+--------+----------+
1 row in set (0.00 sec)
聚合函數
總數
count(*) 表示計算總行數,括號中寫星與列名,結果是相同的
mysql> select count(*) from classes;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
最大值
max(列) 表示求此列的最大值
mysql> select max(salary) from classes;
+-------------+
| max(salary) |
+-------------+
| 13850 |
+-------------+
1 row in set (0.00 sec)
最小值
mysql> select min(salary) from classes;
+-------------+
| min(salary) |
+-------------+
| 7200 |
+-------------+
1 row in set (0.00 sec)
求和
sum(列) 表示求此列的和
mysql> select sum(salary) from classes;
+-------------+
| sum(salary) |
+-------------+
| 50750 |
+-------------+
1 row in set (0.00 sec)
平均工資
mysql> select sum(salary)/count(*) from classes;
+----------------------+
| sum(salary)/count(*) |
+----------------------+
| 8458.333333333334 |
+----------------------+
1 row in set (0.00 sec)
平均值
mysql> select avg(age) from classes;
+----------+
| avg(age) |
+----------+
| 21.2000 |
+----------+
1 row in set (0.01 sec)
分組查詢(group by)
分組查詢是對數據按照某個或多個字段進行分組,MySQL中使用group by關鍵字對數據進行分組,基本語法形式爲:
group by 字段 創建分組
Group by 關鍵字通常和集合函數
或group_concat
一起使用,例如:
MAX()、MIN()、COUNT()、SUM()、AVG()
按年齡age分組
mysql> select age from classes group by age;
+------+
| age |
+------+
| NULL |
| 18 |
| 20 |
| 22 |
| 26 |
+------+
5 rows in set (0.00 sec)
group by + group_concat()
group by + group_concat(字段名)
結合一起使用,
用來查詢在該分組下指定字段的集合;
例如:
mysql> select age,group_concat(name) from classes group by age;
+------+--------------------+
| age | group_concat(name) |
+------+--------------------+
| NULL | cao |
| 18 | li |
| 20 | zheng,yu |
| 22 | wu |
| 26 | zhou |
+------+--------------------+
5 rows in set (0.00 sec)
group by + 聚合函數
和group concat()類似,結合函數查詢該分組集合的情況
mysql> select age,avg(salary) from classes group by age;
+------+-------------+
| age | avg(salary) |
+------+-------------+
| NULL | NULL |
| 18 | 13850 |
| 20 | 7850 |
| 22 | 9200 |
| 26 | 12000 |
+------+-------------+
5 rows in set (0.00 sec)
mysql>
mysql> select age,count(*) from classes group by age;
+------+----------+
| age | count(*) |
+------+----------+
| NULL | 1 |
| 18 | 1 |
| 20 | 2 |
| 22 | 1 |
| 26 | 1 |
+------+----------+
5 rows in set (0.00 sec)
group by + having
having 條件表達式:用來過濾分組結果
having作用和where類似,但having只能用於group by 而where是用來過濾表數據
如下,查詢年齡分組的平均工資且平均超過10000的項
mysql> select age,avg(salary) from classes group by age having avg(salary) > 10000;
+------+-------------+
| age | avg(salary) |
+------+-------------+
| 18 | 13850 |
| 26 | 12000 |
+------+-------------+
2 rows in set (0.00 sec)
mysql>
group by + with rollup
with rollup的作用是:在最後新增一行,來記錄當前表中該字段對應的操作結果,即統計記錄數量,一般是彙總結果。
mysql> select salary,count(*) from classes group by salary with rollup;
+--------+----------+
| salary | count(*) |
+--------+----------+
| NULL | 1 |
| 7200 | 1 |
| 8500 | 1 |
| 9200 | 1 |
| 12000 | 1 |
| 13850 | 1 |
| NULL | 6 |
+--------+----------+
7 rows in set (0.01 sec)
連接查詢
連接是關係數據庫模型的主要特點。連接查詢是關係數據庫中最主要的查詢,主要包括內連接
、外連接
。
內連接
內連接(inner join)使用比較運算符
進行表間某些列數據的比較操作,並列出這些表中與連接條件相匹配的數據行
,組合成新紀錄
,也就是說,在內連接查詢中,只有滿足條件的記錄才能出現在結果關係中。
語法:
select * from 表1 inner join 表2 on 表1.列 運算符 表2.列
mysql> select * from class inner join classes on class.name = classes.name;
+----+------+------+----------+--------+----+------+------+--------+----------+
| id | name | age | location | salary | id | name | age | salary | LOCATION |
+----+------+------+----------+--------+----+------+------+--------+----------+
| 4 | cao | 20 | aaa | 8500 | 6 | cao | NULL | NULL | NULL |
| 11 | wu | NULL | NULL | NULL | 2 | wu | 22 | 9200 | bbb |
+----+------+------+----------+--------+----+------+------+--------+----------+
2 rows in set (0.00 sec)
外連接
外連接查詢將將查詢多個表中相關聯的行,返回結果中不僅包含符合連接條件的行,而且還包含左表(左外連接或左連接)、右表(右外連接或右連接),所以說,左連接和右連接都屬於外連接,相比內連接時,返回查詢結果集合中的僅是符合查詢條件和連接條件的行。
語法
select * from 表1 outer join 表2 on 表1.列 運算符 表2.列
左連接
Left join(左連接):返回包括左表中的所有記錄和右表中連接字段相等的記錄
mysql> select * from class left (outer) join classes on class.name = classes.name
e;
+----+------+------+----------+--------+------+------+------+--------+----------+
| id | name | age | location | salary | id | name | age | salary | LOCATION |
+----+------+------+----------+--------+------+------+------+--------+----------+
| 11 | wu | NULL | NULL | NULL | 2 | wu | 22 | 9200 | bbb |
| 4 | cao | 20 | aaa | 8500 | 6 | cao | NULL | NULL | NULL |
| 1 | zhao | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | qian | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | sun | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | wei | 18 | bbb | 7000 | NULL | NULL | NULL | NULL | NULL |
| 6 | yang | 21 | ccc | 11000 | NULL | NULL | NULL | NULL | NULL |
| 10 | wang | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 12 | liu | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+----+------+------+----------+--------+------+------+------+--------+----------+
9 rows in set (0.00 sec)
右連接
Right join(右連接):返回包括右表中的所有記錄和左表中連接字段相等的記錄
mysql> mysql> select class right (outer) join classes on class.name = classes.name;
+------+------+------+----------+--------+----+-------+------+--------+----------+
| id | name | age | location | salary | id | name | age | salary | LOCATION |
+------+------+------+----------+--------+----+-------+------+--------+----------+
| 4 | cao | 20 | aaa | 8500 | 6 | cao | NULL | NULL | NULL |
| 11 | wu | NULL | NULL | NULL | 2 | wu | 22 | 9200 | bbb |
| NULL | NULL | NULL | NULL | NULL | 1 | zhou | 26 | 12000 | aaa |
| NULL | NULL | NULL | NULL | NULL | 3 | zheng | 20 | 8500 | ccc |
| NULL | NULL | NULL | NULL | NULL | 4 | li | 18 | 13850 | ddd |
| NULL | NULL | NULL | NULL | NULL | 5 | yu | 20 | 7200 | NULL |
+------+------+------+----------+--------+----+-------+------+--------+----------+
6 rows in set (0.00 sec)
複合條件連接查詢
複合條件連接查詢是在連接查詢的過程中,通過添加過濾條件
,限制查詢的結果,使查詢的結果更加準確
例如:
mysql> select * from class inner join classes on class.id = classes.id order by class.id desc;
+----+------+------+----------+--------+----+-------+------+--------+----------+
| id | name | age | location | salary | id | name | age | salary | LOCATION |
+----+------+------+----------+--------+----+-------+------+--------+----------+
| 6 | yang | 21 | ccc | 11000 | 6 | cao | NULL | NULL | NULL |
| 5 | wei | 18 | bbb | 7000 | 5 | yu | 20 | 7200 | NULL |
| 4 | cao | 20 | aaa | 8500 | 4 | li | 18 | 13850 | ddd |
| 3 | sun | NULL | NULL | NULL | 3 | zheng | 20 | 8500 | ccc |
| 2 | qian | NULL | NULL | NULL | 2 | wu | 22 | 9200 | bbb |
| 1 | zhao | NULL | NULL | NULL | 1 | zhou | 26 | 12000 | aaa |
+----+------+------+----------+--------+----+-------+------+--------+----------+
6 rows in set (0.00 sec)
mysql>
mysql> select * from class inner join classes on class.id = classes.id and class.id=2;
+----+------+------+----------+--------+----+------+------+--------+----------+
| id | name | age | location | salary | id | name | age | salary | LOCATION |
+----+------+------+----------+--------+----+------+------+--------+----------+
| 2 | qian | NULL | NULL | NULL | 2 | wu | 22 | 9200 | bbb |
+----+------+------+----------+--------+----+------+------+--------+----------+
1 row in set (0.00 sec)
子查詢
子查詢指一個查詢語句嵌套在另一個查詢語句內部的查詢
,這個特性從mysql4.1開始引入。在select子句中先計算子查詢,子查詢結果作爲外層另一個查詢的過濾條件,查詢可以基於一個表或者多個表
。子查詢中常用的操
作符有any(some)、all、in、exists。子查詢可以添加到select、update和delete語句中,而且可以進行多層嵌套。子查詢中也可以使用比較運算符,如“<”,“<=”,“>”,“>=”和“!=”等。
帶any、some關鍵字的子查詢
any和some關鍵字是同義詞,表示滿足其中任一條件
,它們允許創建一個表達式對子查詢的返回值列進行比較,只要滿足內層子查詢中的任何一個比較條件,就返回一個結果作爲外層查詢的條件。
例:
先創建2個新表再插入數據
mysql> create table tb1(num1 int not null);
Query OK, 0 rows affected (0.03 sec)
mysql> create table tb2(num2 int not null);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into tb1 values(1),(5),(13),(27);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> insert into tb2 values(6),(14),(11),(20);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
查詢tb1表裏面有大於tb2的值
mysql> select num1 from tb1 where num1 > any (select num2 from tb2);
+------+
| num1 |
+------+
| 13 |
| 27 |
+------+
2 rows in set (0.01 sec)
帶all關鍵字的子查詢
all關鍵字與any和some不同,使用all時需要同時滿足所有內層查詢的條件
查詢tb1裏面的值都大於tb2的值
mysql> select num1 from tb1 where num1 > all (select num2 from tb2);
+------+
| num1 |
+------+
| 27 |
+------+
1 row in set (0.00 sec)
正則表達式查詢
正則表達式通常被用來檢索或替換那些符合某個模式的文本內容,根據指定的匹配模式匹配文本中符合要求的特殊字符串;
mysql中使用regexp關鍵字
指定正則表達式的字符匹配模式.
正則選項參考如下表:
例子:
mysql> select * from classes where name regexp '^l';
+----+------+------+--------+----------+
| id | name | age | salary | LOCATION |
+----+------+------+--------+----------+
| 4 | li | 18 | 13850 | ddd |
+----+------+------+--------+----------+
1 row in set (0.00 sec)
mysql> select * from class where name regexp 'ya.*|wa.*';
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 6 | yang | 21 | ccc | 11000 |
| 10 | wang | NULL | NULL | NULL |
+----+------+------+----------+--------+
2 rows in set (0.00 sec)
mysql> select * from class where location regexp 'b{2,}';
+----+------+------+----------+--------+
| id | name | age | location | salary |
+----+------+------+----------+--------+
| 5 | wei | 18 | bbb | 7000 |
+----+------+------+----------+--------+
1 row in set (0.00 sec)