MySQL簡單操作之用select查詢表中的記錄
上次我們介紹了:MySQL簡單操作之a(chǎn)lter table改變表的結(jié)構(gòu),今天我們介紹一下MySQL數(shù)據(jù)庫用select語句查詢表中的記錄的過程,接下來我們就一起來了解一下這部分內(nèi)容。
首先我們來看一下select 語句的基本語法,基本語法如下:
- select selection_list 選擇那些列
- from table_list 從那個(gè)表去選擇行
- where primary_constraint 行必須滿足的條件
- group by grouping_columns 對結(jié)果如何分組
- having secondary_constraint 行必須滿足的第二條件
- order by sorting_colums 對結(jié)果如何排序
- limit count 結(jié)果限定
注意:所有使用的關(guān)鍵詞必須精確地以上面的順序給出,例如,一個(gè)having子句必須跟在groug by子句之后和order by子句之前。
一、普通查詢
1. 檢索出表中所有的數(shù)據(jù)
mysql>select * from pet;
注: 檢索出pet表中所有的數(shù)據(jù)
2. 查詢特定的行
mysql>select * from pet where name="Bowser";
注: 檢索出pet表中, name等于"Bowser" 的這行數(shù)據(jù)
3. 查詢特定的列
select name,birth from pet where owner="Gwen";
注: 在pet表中, 選擇 owner 為"Gwen" 的那行, 只檢索出這一行的name 和 birth 兩列數(shù)據(jù).
二、條件查詢
1. 大于/小于 條件(<,>,=)
mysql>select * from pet where age>="5";
注: 在pet表中, 檢索出age大于等于5 的所有數(shù)據(jù)
2. 組合條件(and/or)
mysql>select * from pet where age>=5 and sex="f";
注: 在pet表中, 檢索出age大于等于5并且sex為"f"的所有的數(shù)據(jù).
mysql>select from * from pet where age >="5" or sex="f";
注: 在pet表中, 檢索出age大于等于5 或者sex為"f"的所有數(shù)據(jù)
mysql>select * from pet where (age>"5" and sex="f") or (name="tom" and sex="m");
注: 在pet表中, 檢出出 age大于5 并且sex為"f" 或者 name為"tom"并且sex為"m"的所有的數(shù)據(jù);
三、查詢排序
格式為: order by column_name [asc/desc] [...]
注:asc表示升序, 為默認(rèn)值, desc為降序. order by不能按text,text和image數(shù)據(jù)類型進(jìn)行排序。
mysql>select name,birth from pet order by birth;
注:在pet表中,檢索出name和birth這兩列,并且按birth以升序排列。
mysql>select name,birth from pet order by birth desc;
注:在pet表中,檢索出name和birth這兩列,并且按birth以降序排列。
mysql>select name, age from pet order by name,birth;
注:在pet表中,檢索出name和age的這兩列的數(shù)據(jù),并先按name來排序,后再按birth來排序。
關(guān)于MySQL數(shù)據(jù)庫用select語句查詢表中的記錄的方法就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!下一次我們將會(huì)介紹:MySQL簡單操作之用update和delete來修改和刪除數(shù)據(jù)。
【編輯推薦】






