07--MySQL自学教程:DQL(Data Query Language:数据库查询语言)简介、基础查询、条件查询、模糊查询以及排序(一)
1. DQL(Data Query Language:數(shù)據(jù)庫(kù)查詢語(yǔ)言)知識(shí)點(diǎn)概覽
DQL(Data Query Language:數(shù)據(jù)庫(kù)查詢語(yǔ)言):
用來(lái)查詢數(shù)據(jù)(記錄),不會(huì)對(duì)數(shù)據(jù)進(jìn)行改變,而是讓數(shù)據(jù)庫(kù)發(fā)送結(jié)果集給客戶端概覽:
2.基本語(yǔ)法
select 列名1,列名2…….列名n from 表名 where 條件 group by 列名 having 條件 order by 列名;
這個(gè)SQL語(yǔ)句包含了DQL部分所有的查詢知識(shí)點(diǎn)。簡(jiǎn)單點(diǎn)說(shuō),只要記住這句話,查詢部分的語(yǔ)法基本就都掌握了。
下面我們將對(duì)各個(gè)部分分開進(jìn)行詳細(xì)說(shuō)明。
3.查詢方法概覽
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)和表格,方便查詢演示。代碼如下:
這里我是使用SQLyog創(chuàng)建的表格,使用cmd命令窗口也可以直接執(zhí)行下列語(yǔ)句,后面的所有演示都是使用該軟件寫的,方便進(jìn)行說(shuō)明和注釋
-- 創(chuàng)建數(shù)據(jù)庫(kù)和表格方便演示使用 CREATE DATABASE queryDemo; -- 創(chuàng)建數(shù)據(jù)庫(kù)queryDemo USE queryDemo; -- 進(jìn)入queryDemo數(shù)據(jù)庫(kù) -- 在queryDemo中創(chuàng)建表格demo1Student CREATE TABLE demo1Student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),age INT,score INT);-- 插入數(shù)據(jù) INSERT INTO demo1Student (NAME,age,score) VALUES ("張三",18,89); INSERT INTO demo1Student (NAME,age,score) VALUES ("李四",23,77); INSERT INTO demo1Student (NAME,age,score) VALUES ("王五",19,69); INSERT INTO demo1Student (NAME,age,score) VALUES ("趙六",18,89); INSERT INTO demo1Student (NAME,age,score) VALUES ("田七",20,92); INSERT INTO demo1Student (NAME,age,score) VALUES ("小明",22,85); INSERT INTO demo1Student (NAME,age,score) VALUES ("小紅",25,97);4.基礎(chǔ)查詢
代碼演示
-- 1.1查詢所有列: SELECT * FROM demo1Student; -- 1.2查詢指定列: SELECT id,NAME FROM demo1Student; SELECT NAME,age,score FROM demo1Student;效果圖:
SELECT * FROM demo1Student;
SELECT id,NAME FROM demo1Student;
SELECT NAME,age,score FROM demo1Student;
5.條件查詢
代碼演示
-- 條件查詢 運(yùn)算符 關(guān)鍵字-- 1.運(yùn)算符 =、!=、<>、<、<=、>、>=;-- 這些都是普通的數(shù)學(xué)運(yùn)算符,就就不做說(shuō)明了, <>:表示不等于SELECT * FROM demo1Student WHERE id=5;SELECT * FROM demo1Student WHERE age!=18;SELECT * FROM demo1Student WHERE age<>18;SELECT * FROM demo!student WHERE score>90;SELECT NAME,age FROM demo1Student WHERE NAME="張三";-- 2.BETWEEN…AND; IN(set); IS NULL; IS NOT NULL; AND;OR;NOT-- 在...之間 與set集合交集 是否為空 是否為非空 并且 或 非SELECT * FROM demo1Student WHERE id BETWEEN 2 AND 4; -- id在2到4之間的學(xué)生-- in(set):指與數(shù)據(jù)庫(kù)中數(shù)據(jù)與set的交集,獲得包含有set中數(shù)據(jù)的記錄(行)SELECT * FROM demo1Student WHERE age IN(25,22,30);SELECT * FROM demo1Student WHERE NAME IN ("小","張三","哈哈");-- 檢驗(yàn)null, 所以添加一個(gè)新數(shù)據(jù)INSERT INTO demo1student VALUES (8,"小花",20,NULL);SELECT * FROM demo1Student WHERE score IS NULL; -- 分?jǐn)?shù)為空的學(xué)生SELECT * FROM demo1Student WHERE score IS NOT NULL; -- 分?jǐn)?shù)不為空的學(xué)生SELECT * FROM demo1Student WHERE age=18 AND score>78; SELECT * FROM demo1Student WHERE age=22 OR age=25; SELECT * FROM demo1Student WHERE NOT age=18; -- age不是18歲的學(xué)生運(yùn)行結(jié)果:
運(yùn)算符
SELECT * FROM demo1Student WHERE id=5;
SELECT * FROM demo1Student WHERE age!=18;
SELECT * FROM demo1Student WHERE age<>18;
SELECT * FROM demo1student WHERE score>90;
SELECT NAME,age FROM demo1Student WHERE NAME=”張三”;
關(guān)鍵字
SELECT * FROM demo1Student WHERE id BETWEEN 2 AND 4; – id在2到4之間的學(xué)生
– in(set):指與數(shù)據(jù)庫(kù)中數(shù)據(jù)與set的交集,獲得包含有set中數(shù)據(jù)的記錄(行)
SELECT * FROM demo1Student WHERE age IN(25,22,30);
SELECT * FROM demo1Student WHERE NAME IN (“小”,”張三”,”哈哈”);
SELECT * FROM demo1Student WHERE score IS NULL; – 分?jǐn)?shù)為空的學(xué)生
SELECT * FROM demo1Student WHERE score IS NOT NULL; – 分?jǐn)?shù)不為空的學(xué)生
SELECT * FROM demo1Student WHERE age=18 AND score>78;
SELECT * FROM demo1Student WHERE age=22 OR age=25;
SELECT * FROM demo1Student WHERE NOT age=18; – age不是18歲的學(xué)生
6.模糊查詢
代碼演示
-- 3.模糊查詢-- _ 任意一個(gè)字符SELECT * FROM demo1student WHERE age LIKE "2_";SELECT * FROM demo1student WHERE age LIKE "_8";SELECT * FROM demo1student WHERE NAME LIKE "小_";-- %:任意0~n個(gè)字符SELECT * FROM demo1student WHERE age LIKE "%8";SELECT * FROM demo1student WHERE NAME LIKE "%小%";運(yùn)行結(jié)果
– _ 任意一個(gè)字符
SELECT * FROM demo1student WHERE age LIKE “2_”;
SELECT * FROM demo1student WHERE age LIKE “_8”;
SELECT * FROM demo1student WHERE NAME LIKE “小_”;
– %:任意0~n個(gè)字符
SELECT * FROM demo1student WHERE age LIKE “%8”;
SELECT * FROM demo1student WHERE NAME LIKE “%小%”;
7.排序
代碼演示
-- 4.排序order by -- 升序ascSELECT * FROM demo1student WHERE score>80 ORDER BY score ASC;-- 降序descSELECT * FROM demo1student WHERE score>80 ORDER BY score DESC;運(yùn)行結(jié)果
– 升序asc
SELECT * FROM demo1student WHERE score>80 ORDER BY score ASC;
– 降序desc
SELECT * FROM demo1student WHERE score>80 ORDER BY score DESC;
8.其他四種排序見下節(jié)
地址:http://blog.csdn.net/baidu_37107022/article/details/72627463
總結(jié)
以上是生活随笔為你收集整理的07--MySQL自学教程:DQL(Data Query Language:数据库查询语言)简介、基础查询、条件查询、模糊查询以及排序(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一张图看透JavaScript原型关系:
- 下一篇: HTML自学教程--一天搞定HTML(总