MySQL學習系列之三——不做限制的查詢

在上一篇內(nèi)容中介紹了對表字段的增加、修改、刪除操作。

在本篇內(nèi)容中,我們將會介紹一些簡單的查詢語句。具體包括全表查詢,查詢部分字段,返回固定條數(shù)的查詢以及對查詢結(jié)果進行排序。

由于還沒有介紹如何在表里插入數(shù)據(jù),所以下面給出建表語句和插入數(shù)據(jù)的語句,方便大家練習。

建表語句:

CREATE TABLE customers

(

cust_id int NOT NULL AUTO_INCREMENT,

cust_name char(50) NOT NULL ,

cust_address char(50) NULL ,

cust_city char(50) NULL ,

cust_state char(5) NULL ,

cust_zip char(10) NULL ,

cust_country char(50) NULL ,

cust_contact char(50) NULL ,

cust_email char(255) NULL ,

PRIMARY KEY (cust_id)

);

插入數(shù)據(jù)語句:

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)

VALUES(10001, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com');

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)

VALUES(10002, 'Mouse House', '333 Fromage Lane', 'Columbus', 'OH', '43333', 'USA', 'Jerry Mouse');

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)

VALUES(10003, 'Wascals', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'rabbit@wascally.com');

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)

VALUES(10004, 'Yosemite Place', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Y Sam', 'sam@yosemite.com');

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)

VALUES(10005, 'E Fudd', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'E Fudd');

在數(shù)據(jù)庫中執(zhí)行完成后,在表customers中就有了對應的數(shù)據(jù),以便我們練習查詢。

1.全表查詢:使用select關鍵字。用 * 表示需要查詢的表中所有的字段。

基本語法:select * from 表名

例如:查詢customers表中所有的字段

select * from customers;

2.查詢表中部分字段

基本語法:select 字段1,字段2,...from 表名

例如:查詢customers表中cust_name 和cust_city 兩個字段

select cust_name, cust_city from customers;

3.去重查詢 關鍵字:distinct

在對結(jié)果進行查詢的時候,有時候,我們想要知道該字段有多少不重復的值,這時候我們就需要對查詢結(jié)果進行去重。

基本語法:select distinct 字段1,字段2 from 表名

例如:我們想要知道客戶所在的cust_city 分別有哪些值

select distinct cust_city from customers;

需要注意的是,distinct對其后跟隨的所有字段都生效。

例如:select distinct cust_city,cust_name from customers;

只要兩條數(shù)據(jù)中,cust_city和cust_name 任何一個值不相同,兩條數(shù)據(jù)都會被查詢出來。

4.對查詢結(jié)果進行限制 關鍵字:limit

在查詢的過程中,我們并不需要一次返回所有的結(jié)果,這時候,我們需要對返回結(jié)果數(shù)進行限制。

例如:我們需要返回查詢結(jié)果前三行

select * from customers limit 3;

在這里,3表示返回的結(jié)果數(shù)從第一行開始不多于3行。

那么,如果我們想要得到第二到四行數(shù)據(jù)怎么辦呢?

select * from customers limit 1,3;

在數(shù)據(jù)庫中,數(shù)據(jù)條數(shù)是從0開始計算的,所以,在上述語句中,1表示第二行,3表示需要返回的數(shù)據(jù)條數(shù)。limit 1,3表示從第二行開始返回三條數(shù)據(jù)。行數(shù)不夠時,系統(tǒng)會返回能返回的所有數(shù)據(jù)。

5.對查詢結(jié)果進行排序 關鍵字:order by

例如:我們需要以cust_id為排序依據(jù)進行排序

select cust_id,cust_name from customers order by cust_id;

如果需要以多個字段為排序依據(jù),可以在order by 后面跟多個字段,中間用 , 隔開。

6.指定排序方向 關鍵字 asc desc

數(shù)據(jù)庫默認的排序方式為從小到大(正向)排序,有時候我們需要對查詢結(jié)果進行從大到?。嫦颍┡判?,這時候,可以使用關鍵字desc。

select cust_id,cust_name from customers order by cust_id desc;

asc表示從小到大(正向)排序,與系統(tǒng)默認一致。

有時候我們會有一些需求,比如按照cust_id從小到大(正向)排序,但是需要按照cust_name從大到小(逆向)排序,可以寫成如下方式:

select * from customers order by cust_name desc,cust_id;

就可以滿足我們的需求。

limit 和order by 混合使用,可以查詢出最值,比如,部門工資最高的員工信息,部門工資排前三的員工信息等。

這一篇的內(nèi)容就這么多啦,喜歡的小伙伴們麻煩點個贊呀,要是可以轉(zhuǎn)發(fā)就更好了。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容