时间:2021-07-01 10:21:17 帮助过:19人阅读
二、单表查询
SELECT NUMBER, NAME, SEX, AGE, HOME FROM STUDENT; #查询所有字段 SELECT * FROM STUDENT; #使用*查询所有字段 SELECT NUMBER, NAME, HOME FROM STUDENT; #查询指定字段View Code
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; #逻辑或View Code
消除字段的重复记录。
SELECT DISTINCT AGE FROM STUDENT;View Code
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;View Code
三、集合函数
SELECT COUNT(*) FROM STUDENT; SELECT SUM(AGE) FROM STUDENT; SELECT AVG(AGE) FROM STUDENT; SELECT MAX(AGE) FROM STUDENT; SELECT MIN(AGE) FROM STUDENT;View Code
四、连接查询
连接查询是将两个或以上的表按某条件连接起来,从中选取需要的数据。连接查询是查询两个或以上表时使用的。当两个表存在相同意义字段时,可以通过该字段来连接。
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;View Code
外连接查询包括左连接查询和右连接查询。
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;View Code
五、子查询
#带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);View Code
六、合并查询结果
SELECT NUMBER FROM STUDENT UNION SELECT NUMBER FROM GRADE; SELECT NUMBER FROM STUDENT UNION ALL SELECT NUMBER FROM GRADE;View Code
七、别名
SELECT * FROM STUDENT S WHERE S.NUMBER >= 30; #为表取别名 SELECT NUMBER AS STUDENT_ID FROM STUDENT; #为字段取别名(AS可有可无)View Code
字段别名无法作为查询条件。
八、正则表达式使用
语法和Java基本一致。
SELECT * FROM STUDENT WHERE HOME REGEXP ‘^G‘;View Code
MySQL笔记:数据查询
标签:开始 golden 包括 data 匹配 显示 并且 范围 view