MySQL筆記:數據查詢
阿新 • • 發佈:2018-03-19
any isp left sts 是否為空 則表達式 默認 sed post
一、基本查詢語句
- 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 TINYINTView CodeNOT 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, HOMEView CodeFROM 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 VALUESView Code(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;View Code
- 使用分組查詢時,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;View Code
三、集合函數
- 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;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
六、合並查詢結果
- UNION:將所有查詢結果合並,去除相同的記錄。
- UNION ALL:將所有結果簡單合並,不去除相同記錄。
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筆記:數據查詢