一、基本查询语句
- WHERE:按照指定的条件进行查询,如果没有WHERE默认查询所有记录。
- GROUP BY:按照指定字段进行分组,若存在HAVING则只有满足HAVING指定的条件才能输出。
- ORDER BY:按照指定字段进行排序,支持ASC和DESC排序方式。默认情况下为ASC(升序)。
CREATE DATABASE MYDB; USE MYDB; CREATE TABLE STUDENT ( NUMBER INT PRIMARY KEY, NAME CHAR(20) NOT NULL, SEX BOOLEAN NOT NULL, AGE TINYINT NOT NULL, HOME CHAR(30) ); SELECT NUMBER, NAME FROM STUDENT WHERE AGE > 18 GROUP BY SEX ORDER BY NUMBER DESC; DROP DATABASE MYDB;
二、单表查询
SELECT NUMBER, NAME, SEX, AGE, HOME FROM STUDENT; #查询所有字段 SELECT * FROM STUDENT; #使用*查询所有字段 SELECT NUMBER, NAME, HOME FROM STUDENT; #查询指定字段
- IN:判断字段的值是否在指定的集合中,若在指定集合中则满足条件。
- BETWEEN AND:判断字段的值是否在指定范围内,若在指定范围内则满足条件。
- LIKE:判断字符串是否相等,若相等则满足条件。
- IS NULL:判断字段是否为空值,若为空值则满足条件。
INSERT INTO STUDENT VALUES (30, ‘Curry‘, 0, 30, ‘Golden State‘); INSERT INTO STUDENT VALUES (23, ‘James‘, 0, 33, ‘Cleveland‘); INSERT INTO STUDENT VALUES (11, ‘Irving‘, 0, 26, ‘Boston‘); SELECT * FROM STUDENT WHERE NAME IN (‘Curry‘, ‘Irving‘); SELECT * FROM STUDENT WHERE NUMBER BETWEEN 10 AND 30; SELECT * FROM STUDENT WHERE NAME LIKE ‘James‘; SELECT * FROM STUDENT WHERE HOME IS NOT NULL; #非空字段 SELECT * FROM STUDENT WHERE SEX = 0 AND AGE >= 30; #逻辑与 SELECT * FROM STUDENT WHERE SEX = 0 OR AGE >= 35; #逻辑或
消除字段的重复记录。
SELECT DISTINCT AGE FROM STUDENT;
- 使用分组查询时,GROUP BY关键字只会显示每个分组的一条记录。
- GROUP BY与GROUP_CONTACT函数同时使用时,每个分组中的指定字段都会显示。
- GROUP BY与HAVING同时使用时,可以限制输出的结果。
- GROUP BY按多个字段进行分组时,按照字段声明的顺序依次分组。
- GROUP BY与WITH ROLLUP同时使用时,会在所有记录的最后加上一条前面记录总和的记录。
- GROUP BY与LIMIT同时使用时,可以限制显示记录的条数,并且支持指定开始位置。
SELECT SEX, GROUP_CONCAT(NAME) FROM STUDENT GROUP BY SEX; SELECT SEX, COUNT(SEX) FROM STUDENT HAVING COUNT(SEX) > 1; SELECT * FROM STUDENT GROUP BY HOME, NUMBER; SELECT AGE, COUNT(AGE) FROM STUDENT GROUP BY AGE WITH ROLLUP; SELECT * FROM STUDENT ORDER BY NUMBER LIMIT 3;
三、集合函数
- COUNT:用于统计记录的数量。
- SUM:计算表中某字段取值的总和。
- AVG:计算表中某字段取值的平均值。
- MAX:求某字段取值的最大值。
- MIN:求某字段取值的最小值。
SELECT COUNT(*) FROM STUDENT; SELECT SUM(AGE) FROM STUDENT; SELECT AVG(AGE) FROM STUDENT; SELECT MAX(AGE) FROM STUDENT; SELECT MIN(AGE) FROM STUDENT;
四、连接查询
连接查询是将两个或以上的表按某条件连接起来,从中选取需要的数据。连接查询是查询两个或以上表时使用的。当两个表存在相同意义字段时,可以通过该字段来连接。
CREATE DATABASE MYDB; USE MYDB; CREATE TABLE STUDENT ( NUMBER INT PRIMARY KEY, NAME CHAR(20) NOT NULL, SEX BOOLEAN NOT NULL, AGE TINYINT NOT NULL, HOME CHAR(30) ); CREATE TABLE GRADE ( NUMBER INT PRIMARY KEY, EXAM_A FLOAT DEFAULT 0, EXAM_B FLOAT DEFAULT 0, EXAM_C FLOAT DEFAULT 0 ); INSERT INTO STUDENT VALUES (30, ‘Curry‘, 0, 30, ‘Golden State‘); INSERT INTO STUDENT VALUES (35, ‘Durant‘, 0, 29, ‘Golden State‘); INSERT INTO STUDENT VALUES (23, ‘James‘, 0, 33, ‘Cleveland‘); INSERT INTO STUDENT VALUES (11, ‘Irving‘, 0, 26, ‘Boston‘); INSERT INTO GRADE VALUES (30, 100, 95, 90); INSERT INTO GRADE VALUES (35, 95, 90, 85); INSERT INTO GRADE VALUES (23, 90, 85, 80); INSERT INTO GRADE VALUES (11, 85, 80, 75); SELECT STUDENT.NUMBER, NAME, EXAM_A, EXAM_B, EXAM_C FROM STUDENT, GRADE WHERE GRADE.NUMBER = STUDENT.NUMBER; DROP DATABASE MYDB;
外连接查询包括左连接查询和右连接查询。
- 左连接可以查询到左表中所有的记录,若右表不存在与左表匹配的记录则显示为空。
- 右连接可以查询到右表中所有的记录,若左表不存在与右表匹配的记录则显示为空。
SELECT NAME, EXAM_A FROM STUDENT LEFT JOIN GRADE ON GRADE.NUMBER = STUDENT.NUMBER; SELECT NAME, EXAM_A FROM STUDENT RIGHT JOIN GRADE ON GRADE.NUMBER = STUDENT.NUMBER;
五、子查询
#带IN关键字的子查询 SELECT NUMBER FROM STUDENT WHERE NUMBER IN (SELECT NUMBER FROM GRADE); #带比较运算符的子查询 SELECT NUMBER FROM STUDENT WHERE NUMBER < (SELECT NUMBER FROM GRADE WHERE NUMBER = 35); #带EXISTS关键字的子查询(当返回true,外层语句继续进行查询) SELECT * FROM STUDENT WHERE EXISTS(SELECT * FROM GRADE WHERE NUMBER = 11); #带ANY关键字的子查询(满足任意条件即可) SELECT * FROM STUDENT WHERE NUMBER >= ANY (SELECT NUMBER FROM GRADE); #带ALL关键字的子查询(必须满足所有条件) SELECT * FROM STUDENT WHERE NUMBER >= ALL (SELECT NUMBER FROM GRADE);
六、合并查询结果
- UNION:将所有查询结果合并,去除相同的记录。
- UNION ALL:将所有结果简单合并,不去除相同记录。
SELECT NUMBER FROM STUDENT UNION SELECT NUMBER FROM GRADE; SELECT NUMBER FROM STUDENT UNION ALL SELECT NUMBER FROM GRADE;
七、别名
SELECT * FROM STUDENT S WHERE S.NUMBER >= 30; #为表取别名 SELECT NUMBER AS STUDENT_ID FROM STUDENT; #为字段取别名(AS可有可无)
字段别名无法作为查询条件。
八、正则表达式使用
语法和Java基本一致。
SELECT * FROM STUDENT WHERE HOME REGEXP ‘^G‘;