詳解SQL中的DQL查詢語(yǔ)言
DQL
DQL:data Query language 數(shù)據(jù)查詢語(yǔ)言
格式:select[distinct] 字段1,字段2 from 表名 where 控制條件
(distinct: 顯示結(jié)果時(shí),是否去除重復(fù)列 給哪一列去重就在哪一列字段前加入distinct)
學(xué)生表
(1)查詢表中的所有信息
SELECT * FROM student
(2)查詢表中的所有學(xué)生姓名和對(duì)應(yīng)的英語(yǔ)成績(jī)
SELECT name,english FROM student
注:可顯示部分字段,如果顯示哪列數(shù)據(jù),就直接寫字段名稱即可
(3) 過(guò)濾表中重復(fù)的math成績(jī)
SELECT DISTINCT math FROM student;
(4) 創(chuàng)建一個(gè)student類 添加屬性id,name,sex,chinese,English,math
并隨機(jī)增加5條屬性
select * from student; – 查詢英語(yǔ)在70到75之間的學(xué)生的信息 -- select * from student where english BETWEEN 70 AND 75; – 查詢語(yǔ)文是80或者82或者90分的學(xué)生信息 -- select * from student where chinese IN(80,82,90); – 查詢所有首字母為l的學(xué)生的成績(jī) -- select * from student where name like "l%"; – 查詢數(shù)學(xué)大于80且語(yǔ)文大于80 的同學(xué) -- select * from student where math>80 and chinese>90; – 對(duì)數(shù)學(xué)成績(jī)排序后輸出 (默認(rèn)升序 ASC) -- select * from student order by math; – 對(duì)數(shù)學(xué)成績(jī)排序后輸出(降序 DESC) -- SELECT * FROM student order by math DESC; – 指定多個(gè)字段進(jìn)行排序,先按第一個(gè)字段進(jìn)行排序,如果相同則按第二個(gè)字段進(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ùn)算符
注:邏輯運(yùn)算符優(yōu)先級(jí) 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對(duì)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)識(shí)
unique 唯一性標(biāo)識(shí)
primary key 主鍵 (auto_increment 設(shè)置自動(dòng)增長(zhǎng)) -- UNIQUE 表約束 唯一性標(biāo)識(shí) -- 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)識(shí) ); desc t4; insert into t4 (name,gender,idCard) VALUE("zs","man","110"); insert into t4 (name,gender,idCard) VALUE("ls","woman","112");
總結(jié)
以上所述是小編給大家介紹的SQL中的DQL查詢語(yǔ)言,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)本站網(wǎng)站的支持!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。