SELECT:查询
SELECT select-list FROM tb WHERE qualification 根据标准qualification查找对应的行
查询语句类型: qualification条件 field领域 distinct独特的
简单查询:
多表查询:
子查询:
SELECT * FROM tb_name: 查询tb_name表的所有信息
SELECT field1,field2 FROM tb_name: 投影显示所设定的领域条目(field),一个field就是一列
SELECT [DISTINCT] * FROM tb_name WHERE qualification;从tb_name表中选择符合条件的独特的不重复的条目。
FROM子句:表、多个表、其他SELECT语句
WHERE子句:布尔关系表达式 =、>、<、<=、>= 表示大于等于,小于等于,大于,小于,等于。
逻辑关系:AND(与&&) OR(或||) NOT(非!) XOR(异或)
mysql>SELECT Name,Age,Gender FROM students WHERE NOT Age>20 AND NOT Gender=‘M‘; #选择查询年龄不大于20,且性别不是男的数据,也可以写成下面的条件
mysql> SELECT Name,Age,Gender FROM students WHERE NOT ( Age>20 OR Gender=‘M‘ );
特殊关系:BETWEEN ... AND ... 在两者之间之间。
LIKE ‘‘
%:任意长度任意字符
_:任意单个字符
Usage: SELECT Name FROM students WHERE Name LIKE ‘Y%‘; 查找Name字段以Y开头的数据。
SELECT Name FROM students WHERE Name LIKE ‘%ing%‘;查找Name字段中必须包括ing的字段
SELECT Name FROM students WHERE Name LIKE ‘Y___‘;查找Name字段中Y后面至少跟着3个字符的
REGEXP或者RLIKE 支持正则表达式:
Usage:SELECT Name,Age FROM students WHERE Name RLIKE ‘^[XY].*$‘;
查找Name字段行首为X或Y的数据
IN离散条件的查找:
Usage: SELECT Name,Age FROM students WHERE Age IN (18,20,25);
查找Age字段为18或20或25岁的相关数据
有空值的时候,比较:IS NULL ,NOT NULL ORDER BY ... 以...某字段升序排列
ORDER BY ... 以...某字段降序排列
Usage: SELECT Name,Age FROM students WHERE Name IS NULL;
查找Name字段为空值的数据
SELECT Name,Age FROM students WHERE Name IS NULL;
查找Name字段不为空值的数据
按照升序或者降序排列出查找的数据:ORDER BY field_name {ASC|DESC};
ASC升序排列(默认值)DESC降序排列
SELECT Name,Age FROM students WHERE CID IS NOT NULL ORDER BY Name;
查找CID字段不为空的数据并按字段Name的升序排列;
SELECT Name,Age FROM students WHERE CID IS NOT NULL ORDER BY Name DESC;
查找CID字段不为空的数据并按字段Name的降序排列;
字段别名:AS
Usage: SELECT Name AS Student_Name FROM student;
mysql> SELECT Name FROM student;
+------------+
| Name |
+------------+
| Li Lianjie |
| Cheng Long |
| Yang Guo |
| Guo Jing |
+------------+
4 rows in set (0.00 sec)
mysql> SELECT Name AS Student_Name FROM student;
+--------------+
| Student_Name | AS别名为Student_Name
+--------------+
| Li Lianjie |
| Cheng Long |
| Yang Guo |
| Guo Jing |
+--------------+
4 rows in set (0.00 sec)
LIMIT子句:LIMIT [offset,]Count offset偏移多少,Count显示多少
Usage: SELECT Name AS Student_Name FROM student LIMIT 3;
查找显示Name别名为Student_Name,只显示前3个数据
Usage: SELECT Name AS Student_Name FROM student LIMIT 2,2;
查找显示Name别名为Student_Name,偏移掉前2个数据不显示,显示第2个数据后的2个数据
mysql> SELECT Name AS Student_Name FROM student LIMIT 3;
+--------------+
| Student_Name |
+--------------+
| Li Lianjie |
| Cheng Long |
| Yang Guo |
+--------------+
3 rows in set (0.00 sec)
mysql> SELECT Name AS Student_Name FROM student LIMIT 2,2;
+--------------+
| Student_Name |
+--------------+
| Yang Guo |
| Guo Jing |
+--------------+
2 rows in set (0.00 sec)
聚合运算:SUM()求和,MIN()最小值,MAX()最大值,AVG()平均值,COUNT()个数;
新建立一张表:
mysql> CREATE TABLE class(ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,Name CHAR(20) NOT NULL UNSIGNED, Age TINYINT NOT NULL);
mysql> INSERT INTO class (Name,Age) VALUES (‘Yang Guo‘,22),(‘Guo Jing‘,46),(‘Xiao Longnv‘,18),(‘Huang Rong‘,40);
mysql> DESC class;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(20) | NO | | NULL | |
| Age | tinyint(4) | NO | | NULL | |
+-------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM class;
+----+-------------+-----+
| ID | Name | Age |
+----+-------------+-----+
| 1 | Yang Guo | 22 |
| 2 | Guo Jing | 46 |
| 3 | Xiao Longnv | 18 |
| 4 | Huang Rong | 40 |
+----+-------------+-----+
4 rows in set (0.00 sec)
mysql> SELECT SUM(Age) FROM class;
+----------+
| SUM(Age) |
+----------+
| 126 |
+----------+
1 row in set (0.02 sec)
mysql> SELECT MAX(Age) FROM class;
+----------+
| MAX(Age) |
+----------+
| 46 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT MIN(Age) FROM class;
+----------+
| MIN(Age) |
+----------+
| 18 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT AVG(Age) FROM class;
+----------+
| AVG(Age) |
+----------+
| 31.5000 |
+----------+
1 row in set (0.00 sec)
分组:GROUP BY ... HAVING qualification 根据...分组 并且满足条件qualification
对于GROUP BY的条件选择需用HAVING作为条件筛选,而不是用WHERE
mysql> SELECT Age,Gender FROM class1 GROUP BY Gender; #以性别分组显示
+-----+--------+
| Age | Gender |
+-----+--------+
| 18 | F |
| 22 | M |
+-----+--------+
2 rows in set (0.00 sec)
mysql> SELECT AVG(Age),Gender FROM class1 GROUP BY Gender; #求男同学和女同学的平均年龄。
+----------+--------+
| AVG(Age) | Gender |
+----------+--------+
| 29.0000 | F |
| 34.0000 | M |
+----------+--------+
2 rows in set (0.00 sec)
mysql> SELECT Name,AVG(Age) FROM class1 GROUP BY Gender HAVING Name RLIKE ‘Y.*‘;
#求以Gender分组的年龄平局值,且只显示以Y开头的Name字段的平均值和姓名
+----------+----------+
| Name | AVG(Age) |
+----------+----------+
| Yang Guo | 34.0000 |
+----------+----------+
1 row in set (0.00 sec)