第一節(jié)已經為大家介紹了數(shù)據庫的基本語法,本節(jié)繼續(xù)為大家講解sql第一條語句:"select 查詢"。
語法如下:
1)select * from 表名? --沒有條件過濾的情況下查詢數(shù)據
2)select * from 表名 where 條件?
--條件過濾的情況下查詢數(shù)據
3)select distinct 列名 from 表名?
--去除重復項的情況下查詢數(shù)據乳愉,DISTINCT 關鍵詞用于返回唯一不同的值。
4)select * from 表名 limit 數(shù)值?
--limit用于限制查詢數(shù)據的條數(shù)屯远,limit 5 表示只查詢5條數(shù)據
如有一個客戶表:customer蔓姚,表有列:cus_id,cus_no,cus_name,cus_age,cus_adds。數(shù)據如下
1慨丐、不帶條件簡單的select查詢語句為:select * from customer; 或者 select cus_id,cus_no,cus_name,cus_age,cus_adds from customer坡脐,兩條語句查詢效果一樣,使用“* from”表示查詢時展示customer表所有列的數(shù)據值房揭。第二種方式也是查詢時展示customer表所有的列值备闲。如果只查詢客戶表customer中幾個值,sql可這樣編寫:
eg:查詢客戶的姓名捅暴,年齡恬砂,地址:select cus_name,cus_age,cus_adds from customer;
2蓬痒、帶條件的sql查詢
eg:查詢姓名為“張三”的客戶信息:select * from customer where cus_name = "張三";
eg:查詢年齡在25周歲的客戶信息:select * from customer where cus_age = 25;
3泻骤、多個關聯(lián)條件時使用 and 或者 or 進行連接
eg:查詢年齡大于25歲且地址在北京市的客戶信息:select * from customer where cus_age > 25 and cus_adds = "北京市";
eg:查詢年齡大于25歲或者地址在北京市的客戶信息:select * from customer where cus_age > 25 or cus_adds = "北京市";
使用where語句過濾數(shù)據時,where子句中運算符使用規(guī)則如下
eg:查詢年齡25梧奢,26狱掂,27,28的客戶信息:select * from customer where cus_age in ('25','26','27','28',);
eg:查詢年齡在20到30之間的客戶信息:select * from customer where cus_age between 20 and 30;? 或者 select * from customer where cus_age >= '20' and cus_age <= '30';
通配符like使用方法這里先暫時不介紹亲轨,后面會有專門專題為大家講解趋惨。
4、distinct 的select 語句查詢
eg:查詢年齡唯一且不同值的客戶信息:select distinct cus_age from customer;
5瓶埋、limit 的select 語句查詢
eg:查詢年齡25歲的客戶信息希柿,只展示10條數(shù)據:select? cus_age from customer limit 10;
eg:查詢年齡25歲的客戶信息诊沪,只展示10條數(shù)據养筒,從第6條開始展示:select? cus_age from customer limit 5,10;
本節(jié)select 查詢語句用法講解就結束了,大家可以在自己電腦參考以上sql語句進行練習端姚,多練習where多條件情況下的查詢晕粪,熟練掌握distinct渐裸,limit的基本語法和使用巫湘。