DQL 語句
DQL(Data QueryLanguage )數據查詢語言,結構化查詢語言
其他的語言
DQL:數據庫查詢語言。語句:SELECT
, FROM
WHERE
。
DDL:數據庫模式定義語言。語句:CREATE,DROP,ALTER。
DML:數據操縱語言。語句:INSERT、UPDATE、DELETE。
DCL:數據控制語言 。語句:GRANT、REVOKE。
TCL:事務控制語言。語句:COMMIT、ROLLBACK、SAVEPOINT。
DDL,DML,DCL,DQL,TCL共同組成數據庫的完整語言。
數據查詢語言
用於對數據庫關係表中的數據進行查詢的語句,支持多種方式多種要求的查詢。
數據查詢語言語句:select
查詢語句一般格式:select 屬性名 from 表名;
查詢表格中的所有內容
select * from 表格;
查詢語句的使用
select
命令可以讀取一條或者多條記錄- 用星號(*) 表示表內的所有列名
- 用
where
語句限定條件 - limit 限定查詢結果
1.查詢所有列元素
select * from 表名;
2.查詢指定列元素
select 屬性名1,屬性名2,…,屬性名n from 表名;
查詢指定的列元素
select s_name,s_id from student_tb;
3.查詢指定字段符合相關條件的記錄
select <屬性名1,屬性名2,…,屬性名n> from <表名> where <表達式>;
通過where 語句進行條件篩選
select * from grade_tb where g_score<60;
MySql通用的 SELECT 語法規則
SELECT <屬性名>,...
FROM <表名>
[WHERE <條件表達式>]
[LIMIT <分頁查詢>]
where表達式
where 語句後面接條件表達式其中運算符有:
符號 | 表達式 | 作用 |
---|---|---|
= | 屬性名 = 數值 | 判斷是否相等 |
!= (<>) | 屬性名 != 數值 | 判斷是否不相等 |
> (>=) | 屬性名 > 數值 | 大於 (大於等於) |
< (<=) | 屬性名 < 數值 | 小於 (小於等於) |
between…and… | 屬性名 between 數值 and 數值 | 在某個範圍內 |
not between…and… | not 同上 | 不在範圍內 |
in | in(數值1,數值2…) | 查找多個匹配的數值 |
like | 屬性名 like ‘%匹配字符%’ | 搜索匹配某種模式(模糊查找) |
and | 表達式1 and 表達式2 | 邏輯與 兩個都爲真爲則表示爲真 |
or | 表達式1 or 表達式2 | 邏輯或 有一個爲一則表示爲真 |
is (not)null | 屬性名 is (not)null | 判斷是否(不)爲空 |
使用條件表達式
#查找學號在1和3之間的學員
select * from student where s_no between 1 and 3;
#查找一個姓李的人
select * from student where s_name like '李%';
where 語句進行條件的過濾和匹配
多條件查詢
與或非語句:and
or
and
相當與C語言中 &&
or
相當於C語言中的 ||
select <屬性名> from student where 表達式1 and 表達式2;
1.在集合範圍內查找
語句:in
select *from student where s_no in(1,6,3);
2.在集合範圍外查找
語句:not in
select *from student where s_no not in(2,4);
3.查詢空值
語句:is null
select * from class where s_birchday is null;
4.查詢非空值
語句:is not null(不是not is null)
select * from class where s_birchday is not null;
5.between …and 語句
在…範圍之內
查詢在60和80之間的數據,60和80也算在內
select * from grade_tb where g_score between 60 and 80;
6.like 語句
% 表示模糊匹配裏面的所有數據(0個或者多個)
_ 表示匹配一個字符的數據
## 找出姓張的老師
select * from teacher_tb where t_name like '張%';
##找出開頭有王,結尾有圓
select * from student_tb where s_name like '王%圓';
常見查詢語句
去重查詢
語句:distinct
## 將查詢的結果進行重複數據篩選
select distinct s_classno from student;
注意:distinct只能對一 個屬性去重,如果是多條數據的話,必須整行數據一致去重
分頁
語句:limit [offset,] row;
查詢從第n條記錄起查詢其後的m條記錄
#查詢前三條數據,offset默認爲0
select * from student limit 3;
##這條表達式相當於
select * from student limit 0,3;
###查詢從3開始,第五條數據
select * from student limit 3,5;
offset爲偏移量,是指從第一條記錄往後移幾條記錄與指針偏移類似。
查詢第2到第四條數據
select * from student limit 1,3;
合併查詢結果
語句
union 語句:用於將不同表中相同列中查詢的數據展示出來;(不包括重複數據)
union all 語句:用於將不同表中相同列中查詢的數據展示出來;(包括重複數據)
使用形式如下:
select <列名稱> from <表名>
union (all)
select <列名稱> from <表名>
用於合併多個select語句的結果,要求第一個sql語句返回的列與第二個sql語句返回的列相同
複製表數據
把一個表的數據複製到另一個已存在且結構相同的表中。
insert into 表1 select * from 表2;
1.通過查詢的結果集向表中插入數據
ALL: 可選,返回所有結果集,包含重複數據。
insert into <表名> select <數據> union all
#例,union的其他應用,容易出錯,不推薦使用
insert into student select 1,'張三' union all
select 2,'李四' union all
select 3,'王五';
2.創建表時複製其他表的數據
create table <表名1>(select * from <表名2>);
也就是創建表格時,不規定它的屬性,而是複製其他表的屬性結構與數據。但是需要注意的是,新表不會複製其他表的主鍵,所以這樣創建的表是沒有主鍵的。(這種方法實現表的備份)
也可只複製其他表的部分數據,改一下select語句即可。
取別名
在查詢時可以給屬性取別名,使得讀取出的數據更直觀。
語句:as
select <屬性名> as <別名> from <表名> as <別名>;
其中語句as可以省略
排序
語句:order by
select * from <表名> order by <屬性名>;
默認爲升序排列,如果需要降序排列,就在語句之後加一個語句desc
。
select * from <表名> order by <屬性名> desc;
總結
查詢語句的基本使用
1.查詢字段
2.條件查詢
3.常見查詢語句