詳解SQL中的DQL查詢語言
DQL
DQL:data Query language 數(shù)據(jù)查詢語言
格式:select[distinct] 字段1,字段2 from 表名 where 控制條件
(distinct: 顯示結果時,是否去除重復列 給哪一列去重就在哪一列字段前加入distinct)
學生表
(1)查詢表中的所有信息
SELECT * FROM student
(2)查詢表中的所有學生姓名和對應的英語成績
SELECT name,english FROM student
注:可顯示部分字段,如果顯示哪列數(shù)據(jù),就直接寫字段名稱即可
(3) 過濾表中重復的math成績
SELECT DISTINCT math FROM student;
(4) 創(chuàng)建一個student類 添加屬性id,name,sex,chinese,English,math
并隨機增加5條屬性

select * from student; – 查詢英語在70到75之間的學生的信息 -- select * from student where english BETWEEN 70 AND 75; – 查詢語文是80或者82或者90分的學生信息 -- select * from student where chinese IN(80,82,90); – 查詢所有首字母為l的學生的成績 -- select * from student where name like "l%"; – 查詢數(shù)學大于80且語文大于80 的同學 -- select * from student where math>80 and chinese>90; – 對數(shù)學成績排序后輸出 (默認升序 ASC) -- select * from student order by math; – 對數(shù)學成績排序后輸出(降序 DESC) -- SELECT * FROM student order by math DESC; – 指定多個字段進行排序,先按第一個字段進行排序,如果相同則按第二個字段進行排序 -- 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支持表達式 加減乘除;
as: 表示給某一列起別名;并且as 可以省略;
– 關聯(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, -- 進行和customer 關聯(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;
主鍵和唯一標識
unique 唯一性標識
primary key 主鍵 (auto_increment 設置自動增長)
-- UNIQUE 表約束 唯一性標識
-- 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 唯一性標識
);
desc t4;
insert into t4 (name,gender,idCard) VALUE("zs","man","110");
insert into t4 (name,gender,idCard) VALUE("ls","woman","112");
總結
以上所述是小編給大家介紹的SQL中的DQL查詢語言,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網(wǎng)站的支持!
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
關注官方微信