新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)SQL中DQL查詢語言的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為江岸企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè),江岸網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
DQL
DQL:data Query language 數(shù)據(jù)查詢語言
格式:select[distinct] 字段1,字段2 from 表名 where 控制條件
(distinct: 顯示結(jié)果時,是否去除重復(fù)列 給哪一列去重就在哪一列字段前加入distinct)
學(xué)生表
(1)查詢表中的所有信息
SELECT * FROM student
(2)查詢表中的所有學(xué)生姓名和對應(yīng)的英語成績
SELECT name,english FROM student
注:可顯示部分字段,如果顯示哪列數(shù)據(jù),就直接寫字段名稱即可
(3) 過濾表中重復(fù)的math成績
SELECT DISTINCT math FROM student;
(4) 創(chuàng)建一個student類 添加屬性id,name,sex,chinese,English,math
并隨機增加5條屬性
select * from student; – 查詢英語在70到75之間的學(xué)生的信息 -- select * from student where english BETWEEN 70 AND 75; – 查詢語文是80或者82或者90分的學(xué)生信息 -- select * from student where chinese IN(80,82,90); – 查詢所有首字母為l的學(xué)生的成績 -- select * from student where name like "l%"; – 查詢數(shù)學(xué)大于80且語文大于80 的同學(xué) -- select * from student where math>80 and chinese>90; – 對數(shù)學(xué)成績排序后輸出 (默認(rèn)升序 ASC) -- select * from student order by math; – 對數(shù)學(xué)成績排序后輸出(降序 DESC) -- SELECT * FROM student order by math DESC; – 指定多個字段進(jìn)行排序,先按第一個字段進(jìn)行排序,如果相同則按第二個字段進(jìn)行排序 -- SELECT * FROM student ORDER BY math DESC,chinese DESC; – WHERE后可以加 ORDER BY -- SELECT * from student where name like "%l" ORDER BY math DESC; – 顯示student 表格中的前3行 SELECT * from student LIMIT 2; – 顯示student 表格中的第3~5行 SELECT * from student LIMIT 2,3; -- 2表示偏移量,3表示顯示的行數(shù)
附錄:①在where中經(jīng)常使用的運算符
注:邏輯運算符優(yōu)先級 not>and>or
*②select |{column1|expression、column2|expression,…}from table; select column as 別名 from table;
注:
expression : MySQL支持表達(dá)式 加減乘除;
as: 表示給某一列起別名;并且as 可以省略;
– 關(guān)聯(lián)(1對N)
create table customer( id int PRIMARY KEY auto_increment, name varchar (20) not null, adress varchar (20) not null ); create table orders( order_num varchar(20) PRIMARY KEY, price FLOAT not NULL, customer_id int, -- 進(jìn)行和customer 關(guān)聯(lián)的字段 外鍵 constraint cus_ord_fk foreign key (customer_id) REFERENCES customer(id) ); insert into customer(name,adress) values("zs","北京"); insert into customer(name,adress) values("ls","上海"); SELECT * from customer; INSERT INTO orders values("010",30.5,1); INSERT INTO orders values("011",60.5,2); INSERT INTO orders values("012",120.5,1); SELECT * from orders;
主鍵和唯一標(biāo)識
unique 唯一性標(biāo)識
primary key 主鍵 (auto_increment 設(shè)置自動增長) -- UNIQUE 表約束 唯一性標(biāo)識 -- PRIMARY KEY 主鍵 CREATE TABLE t4 ( id INT PRIMARY KEY auto_increment, NAME VARCHAR (20) NOT NULL, gender CHAR (5) NOT NULL, idCard VARCHAR (20) UNIQUE -- UNIQUE 唯一性標(biāo)識 ); desc t4; insert into t4 (name,gender,idCard) VALUE("zs","man","110"); insert into t4 (name,gender,idCard) VALUE("ls","woman","112");
關(guān)于“SQL中DQL查詢語言的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文標(biāo)題:SQL中DQL查詢語言的示例分析
標(biāo)題路徑:http://fisionsoft.com.cn/article/jihoeg.html