02 檢索資料

2022-09-02 10:12:07 字數 2367 閱讀 4673

1.select

語句從乙個表或多個表中檢索資訊

2.檢索單個列

輸入:select prod_name

from products;

輸出:

沒有過濾,也沒有排序,輸出資料順序可能不同。

3.檢索多個列

輸入:select prod_id, prod_name, prod_price

from products;

輸出:

4.檢索所有列

輸入:select *

from products;

輸出:

給定乙個萬用字元(

*),則返回列表中所有列。

5.檢索不同的值

輸入:select vend_id

from products;

輸出:

select

語句返回

9行,但只有

3個產品**商因此,使用

distinct

關鍵字輸入:

select distinct vend_id

from products;

輸出:

注意,distinct

關鍵字作用於所有列,而不僅僅是跟在其後的那一列。

輸入:select distinct vend_id, prod_price

from products;

輸出:

和輸入:

select vend_id, prod_price

from products;

的輸出結果:

相比,選擇的時

prod_price

不同的幾行資料。

6.限制結果

輸入:select prod_name

from products

limit 5;

輸出:

只檢索前5行。

limit 5

表示mysql

等dbms

返回不超過

5行的資料。

要檢索後

5行,需要指定從哪開始以及檢索的行數。

輸入:select prod_name

from products

limit 5 offset 5;

輸出:

limit 5 offset 5

指示mysql

等dbms

返回從第

5行起的

5行資料。第乙個數字是檢索的行數,第二個數字是指從哪開始。由於

products只有9

行資料,所以只返回了

4行資料(因為沒有第

5行)。

第乙個被檢索的行是第

0行,而不是第

1行,因此

limit 1 offset 1

會檢索第

2行,而不是第1行。

7.使用注釋

(1)使用

--(兩個連字元)嵌入行內進行注釋:

select prod_name --

這是一條注釋

from products;

(2)另一種行內注釋:

#這是一條注釋

select prod_name

from products;

(3)多行注釋,從

/*開始,到

*/結束:

/*select prod_name, vend_id

from products;*/

select prod_name

from products;

mysql檢索資料

簡單的來說select 語句用於從表中選取資料。select from city idname countrycode district population 1kabul afgkabol 1780000 2qandahar afgqandahar 237500 3herat afgherat 1...

排序檢索資料

select prod name from products 這樣直接檢索出的單個列,並沒有特定的順序 關聯式資料庫設計理論認為,如果不明確規定順序排序,則不應該假定檢索出的資料的順序有任何意義 為了明確的進行排序可使用order by 字句 order by字句出現的位置必須是select語句中最...

MySQL檢索資料

1.檢索單個列 select 列名 from 表名 注意,用於命令列中結束語句,dbms中可不加2.檢索多個列 select from 返回的資料一般是原格式,不改變精度或貨幣表示3.檢索所有列 用 萬用字元,表示模糊查詢 select from 最好不要使用 會影響資料庫的效率。4.檢索不同 行 ...