隨著基礎(chǔ)知識的不斷深入,我們會漸漸發(fā)現(xiàn)一些有趣的概念,覺得:欸,不錯欸,好想繼續(xù)聽下去.保持這種想法,繼續(xù)前行,你會發(fā)現(xiàn)知識不僅僅是紙上談兵
一.字段表達式
看著像查詢語句,實際上是執(zhí)行字段表達式的結(jié)果
1.字段表達式
select 語句;
示例一:
select 66;
select 66 as mul; #起別名
select 66 mul2; #起別名
示例二:
- 以上語句雖然可以執(zhí)行, 但是看上去不符合MySQL查詢語句的規(guī)范
- 如果想要執(zhí)行字段表達式, 又想要符合MySQL查詢語句的規(guī)范, 那么可以使用偽表
- 什么是偽表? 占位符,但是實際上什么都不會做
select 66 mul from dual;
By 極客江南
where子語句:支持簡單運算符:<,>,<=,>=,=,!=,and,or,not
二.查詢語句in/not in
- in | not in
示例一:
需求: 要求找出表中城市在北京和武漢的人
過去的做法:
弊端如果需要查找的城市太多, 語句會變得很冗余
select * from stu where city='北京' or city='武漢';
如果需要查找的城市太多, 可以利用in來簡化語句
select * from stu where city in ('北京', '武漢');
示例二:
需求: 要求找出表中城市不在北京和武漢的人
select * from stu where city!='北京' and city!='武漢';
select * from stu where city not in ('北京', '武漢');
By 極客江南
三.查詢語句between...and/not between...and
4.between...and | not between...and
示例一:
需求: 要求找出表中年齡在17~23歲之間的人
select * from stu where age>=17 and age<=23;
select * from stu where age between 17 and 23;
示例二:
需求: 要求找出表中年齡不在17~23歲之間的人
select * from stu where age<17 or age>23;
select * from stu where age not between 17 and 23;
By 極客江南
四.查詢語句is null/is not null
- is null | is not null
注意點: 在MySQL中判斷某一個字段保存的數(shù)據(jù)是否為null不能用等于符號
select * from stu where age=18;
insert into stu (name) values('it66');
select * from stu where age is null;
select * from stu where age is not null;
By 極客江南
五.查詢語句-模糊查詢
6.模糊查詢
_通配符: 表示任意一個字符
%通配符: 表示任意0~n個字符
a_c: abc, adc
abc,adc,abbc,ac
_a_c: 1abc,3adc
1abc,abc1,2abbc,3adc
a%c:abc, adc,abbc, ac
abc,adc,abbc,ac
%a%c:1abc,2abbc, 3adc
1abc,abc1,2abbc,3adc
格式:
select 字段 from 表名 where 字段 like '條件';
select * from stu where name like 'z_c';
select * from stu where name like 'z%';
By 極客江南
六.查詢語句-排序
7.排序 order by
格式:
select 字段 from 表名 order by 字段 [asc | desc]
示例一:
select * from stu order by age; #默認(rèn)是升序排序
select * from stu order by age asc; #升序
select * from stu order by age desc; #降序
示例二:
insert into stu values(null, 'itzb', 23, 100, '廣州');
需求: 按照年齡升序排序, 如果年齡相同那么按照成績降序排序
select * from stu order by age asc, score desc;
By 極客江南
七.查詢語句-分組
- 數(shù)據(jù)分組 group by: 查文檔自學(xué)一下
select city, avg(score) from stu group by city;
# 如果分組查詢, 那么查詢的字段必須包含分組字段和聚合函數(shù)
# city就是分組字段/avg()就是聚合函數(shù)
#如果查詢的字段不是分組字段, 那么只會返回分組中的第一個值
select name from stu group by city;
#group_concat()函數(shù)可以將每一組中的所有數(shù)據(jù)連接在一起
select group_concat(name) from stu group by city;
在企業(yè)開發(fā)中, 一般情況下使用分組都是用來統(tǒng)計
select city, count(*) from stu group by city;
By 極客江南
八.查詢語句having
- 條件 having: 查文檔自學(xué)一下
\ - 默認(rèn)情況下都是去數(shù)據(jù)庫的表中查詢數(shù)據(jù), 如果想在查詢結(jié)果的基礎(chǔ)上查詢數(shù)據(jù), 那么就可以使用having
- where條件會去表中查詢是否符合條件, having條件會去查詢結(jié)果集中查詢是否符合條件
示例一:
select * from stu where city='武漢'; #去數(shù)據(jù)庫的表中匹配條件
select * from stu having city='武漢'; #去查詢的結(jié)果集中匹配添加
示例二:
\ #可以找到武漢的人, 因為是去數(shù)據(jù)庫的表中匹配
select name,age from stu where city='武漢';
#不可以找到, 因為結(jié)果集中只有name和age,沒有city,所以找不到
select name,age from stu having city='武漢';
\ # 前面部分代碼查詢返回的結(jié)果我們稱之為結(jié)果集
# 如下語句返回的結(jié)果集中包含了name和age
select name,age from stu;
示例三:
需求: 查看表中哪些城市的平均分>=60分
select city , avg(score) as avgscore from stu group by city;
select city , avg(score) as avgscore from stu group by city where avgscore>=60; #報錯, 因為數(shù)據(jù)庫的表中沒有avgscore字段
select city , avg(score) as avgscore from stu group by city having avgscore>=60;
By 極客江南
九.查詢語句-分頁
11 分頁 limit: 查文檔自學(xué)一下
select 字段 from 表 limit 索引, 個數(shù);
示例一:
返回表中的前兩條數(shù)據(jù)
select * from stu limit 0, 2;
select * from stu limit 2;
返回表中的第3條數(shù)據(jù)和第4條數(shù)據(jù)
select * from stu limit 2, 2;
By 極客江南
十.查詢語句-查詢選項
- 查詢選項
all: 顯示所有數(shù)據(jù)[默認(rèn)]
distinct: 取出結(jié)果集中重復(fù)的數(shù)據(jù)
select all name from stu;
select distinct name from stu;
By 極客江南
十一.完整查詢語句
完整的查詢語句
select [查詢選項] 字段名稱 [from 表名] [where 條件] [order by 排序] [group by 分組] [having 條件] [limit 分頁];
By 極客江南