1)IN
BETWEEN AND
>SELECT IDD FROM table1
WHERE (AGE BETWEEN 19 AND 21) AND (SEX IN ('Male')); # 结果如下图2
2)LIKE
>SELECT IDD FROM table1
WHERE (NAME LIKE 'Z%') OR (NAME LIKE 'ZH______'); # 结果如下图3
3)DISTINCT
ORDER BY #排序
>SELECT DISTINCT AGE FROM table1 ORDER BY AGE; #结果如下图4、5
4)ASC DESC #排序
>SELECT AGE,SCORE FROM table1 ORDER BY AGE ASC,SCORE DESC;#先升序再降序 #结果如下图5
5)GROUP BY
>SELECT COUNT(IDD) AS '学号数量' FROM table1
GROUP BY AGE
HAVING COUNT(IDD) > 1; #结果如下图7
6)GROUP_CONCAT() #将分组中的各个字段显示出来
>SELECT COUNT(IDD) AS '学号数量',GROUP_CONCAT(NAME) AS '姓名' FROM table1
GROUP BY AGE
HAVING COUNT(IDD) > 1; #结果如下图8
7)LIMIT
>SELECT * FROM table1 LIMIT 4;#查询前4行
>SELECT * FROM table1 LIMIT 5,3;#从第六行开始查询,查询3行 #结果如下图9
8)内连接
>SELECT *
FROM table1,table2
WHERE table1.IDD = table2.ID;
等价于:
>SELECT *
FROM table1 INNER JOIN table2
ON table1.IDD = table2.ID; #结果如下图10
9)自连接
SELECT *
FROM table1 AS f1,table1 AS f2
WHERE f1.IDD = f2.ID;
10)外连接(左连接、右连接)
#这里两个表没有选好,看不出左右连接的区别
SELECT *
FROM table1 LEFT OUTER JOIN table2
ON table1.IDD = table2.ID; #结果如下图11
11)子查询
ANY = SOME #表示满足某一个
ALL #表示满足所有的
>SELECT IDD FROM table1
WHERE AGE > ANY (SELECT AGE FROM table1); #结果如下图12
……
12)EXISTS
#如果EXISTS有结果则执行外查询,否则不执行。
NOT EXISTS #与EXISTS相反
>SELECT * FROM table1
WHERE EXISTS (SELECT AGE FROM table1); #结果如下图13
13)UNION
#结果合并,对应列数和数据类型必须相同
UNION ALL #表示不去重复
>SELECT IDD ,AGE FROM table1 #结果如下图14
WHERE AGE > 20
UNION ALL
SELECT IDD ,AGE FROM table1
WHERE AGE < 20
14)正则表达式查询
# '^b' 表示以b开头的记录
>SELECT * FROM table1
WHERE NAME REGEXP '^b'; #结果如下图15
# 'i$' 表示以i结尾的记录
>SELECT * FROM table1
WHERE NAME REGEXP 'i$'; #结果如下图15
# 'h.o'表示h-o三个字符匹配的记录
>SELECT * FROM table1
WHERE NAME REGEXP 'h.o'; #结果如下图15
# 'Li+'表示以L开头后面至少跟一个i
>SELECT * FROM table1
WHERE NAME REGEXP 'Li+'; #结果如下图16
# 'L*i'表示i前面有若干个L,可以为0个
>SELECT * FROM table1
WHERE NAME REGEXP 'L*i'; #结果如下图16
# 'iu' 表示包含'iu'的记录
>SELECT * FROM table1
WHERE NAME REGEXP 'iu'; #结果如下图17
#[abc] 表示包含a、b、c中任意一个
#[^abc] 表示不包含a、b、c中所有的?
?????
>SELECT * FROM table1
WHERE NAME REGEXP [abc];
>SELECT * FROM table1
WHERE NAME REGEXP [^abc]; #结果如下图18
#i{2,m}表示包含i连续出现至少2次,最多m次
实验结果图如下:
图2
图3
图4
图5
图6
图7
图8
图9
图10
图11
图12
图13
图14
图15
图16
图17
图18