SQL單表查詢語句

 查詢某些字段不同記錄:
            job字段中,可以發現有相同的數據,爲了查詢有多少種不同的job,在【命令編輯區】輸入“select distinct job  from  scott.emp”   |   select distinct 字段名 from 數據表,這裏的“distinct”保留字指在顯示時去除相同的記錄,與之對應的是“all”將保留相同的記錄,默認爲“all”。

表4.1                                                                        比較運算符
名稱                                                                                                             實例
=(等於)                                                                 select  *  from  scott.emp  where  job=’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal=1100;  
!= (不等於)                                                           select  *  from  scott.emp  where  job!=’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal!=1100;
^=(不等於)                                                           select  *  from  scott.emp  where  job^=’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal^=1100;
<>(不等於)                                                           select  *  from  scott.emp  where  job<>’MANAGER’;
                                                                              select  *  from  scott.emp  where  sal<>1100;
<(小於)                                                                 select  *  from  scott.emp  where  sal<2000;
                                                                              select  *  from  scott.emp  where  job<’MANAGER’;
>(大於)                                                                 select  *  from  scott.emp  where  sal>2000;
                                                                             select  *  from  scott.emp  where  job>’MANAGER’;
<=(小於等於)                                                      select  *  from  scott.emp  where  sal<=2000;
                                                                             select  *  from  scott.emp  where  job<=’MANAGER’;
>=(大於等於)                                                      select  *  from  scott.emp  where  sal>=2000;
                                                                             select  *  from  scott.emp  where  job>=’MANAGER’;
in(列表)                                                               select  *  from  scott.emp  where  sal in (2000,1000,3000);
                                                                            select  *  from  scott.emp  where  job in (’MANAGER’,’CLERK’);
not in(不在列表)                          select  *  from  scott.emp  where  sal not in (2000,1000,3000);
                                                      select  *  from  scott.emp  where  job not in (’MANAGER’,’CLERK’);
between(介於之間)                                          select  *  from  scott.emp  where  sal  between 2000 and 3000;
                                                     select  *  from  scott.emp  where  job  between  ’MANAGER’and CLERK’;
not between                                                      select  *  from  scott.emp  where  sal  not between 2000 and 3000;
(不介於之間)                               select  *  from  scott.emp  where  job  not between  ’MANAGER' and ’CLERK’;
like(模式匹配)                                                   select  *  from  scott.emp  where  job  like  ’M%’;
                                                                            select  *  from  scott.emp  where  job  like  ’M__’;
not like                                                               select  *  from  scott.emp  where  job  not  like  ’M%’;
(模式不匹配)                                                      select  *  from  scott.emp  where  job  not  like  ’M__’;
Is null                                                                 select  *  from  scott.emp  where  sal  is null;
(是否爲空)                                                         select  *  from  scott.emp  where  job  is null;
is not null(是否爲空)                                        select  *  from  scott.emp  where  sal  is  not  null;
                                                                            select  *  from  scott.emp  where  job  is  not  null;

SQL模糊查詢,使用like比較字,加上SQL裏的通配符,請參考以下:
      like和not like適合字符型字段的查詢,%代表任意長度的字符串,_下劃線代表一個任意的字符。like ‘m%’ 代表m開頭的任意長度的字符串,like ‘m__’ 代表m開頭的長度爲3的字符串。      

1、LIKE'Mc%' 將搜索以字母 Mc 開頭的所有字符串(如 McBadden)。
2、LIKE'%inger' 將搜索以字母 inger 結尾的所有字符串(如 Ringer、Stringer)。
3、LIKE'%en%' 將搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 將搜索以字母 heryl 結尾的所有六個字母的名稱(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 將搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 將搜索以字符串 inger 結尾、以從 M 到 Z 的任何單個字母開頭的所有名稱(如 Ringer)。
7、LIKE'M[^c]%' 將搜索以字母 M 開頭,並且第二個字母不是 c 的所有名稱(如MacFeather)。
-------------------------------------------------
呵呵,要完整的例句啊。下面這句查詢字符串是我以前寫的,根據變量 zipcode_key 在郵政編碼表 zipcode 中查詢對應的數據,這句是判斷變量 zipcode_key 爲非數字時的查詢語句,用 % 來匹配任意長度的字符串,從表中地址、市、省三列中查詢包含關鍵字的所有數據項,並按省、市、地址排序。這個例子比較簡單,只要你理解了方法就可以寫出更復雜的查詢語句。

sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address"

注:

"%"可以表示多個字符,
"_"只能表示一個字符,
一個漢字是兩個字符,所以表示漢字應該用兩個_,即“__”

 排序查詢 :
在【命令編輯區】輸入“select empno,ename,job from scott.emp where job<=’CLERK’ order by job asc,sal desc” |      order by 可以指定查詢結果如何排序,形式爲字段名 排序關鍵詞;asc代表升序排列,desc代表降序排列,多個排序字段之間通過逗號分割。若有where查詢條件,order by要放在where語句後面

分組查詢 :
分組查詢是指將查詢結果按照字段分組。
(1)在【命令編輯區】輸入“select  empno,ename,job,sal  from  scott.emp  group  by job,empno,ename,sal having sal<=2000”

(2)在【命令編輯區】輸入“select  empno,ename,job,sal  from  scott.emp  where sal<=2000 group by job,empno,ename,sal”

where檢查每條記錄是否符合條件,having是檢查分組後的各組是否滿足條件。
having語句只能配合group by語句使用,沒有group by時不能使用having,但可以使用
where。

變換查詢顯示
在【命令編輯區】輸入“select  empno 編號,ename 姓名,job 工作,sal 薪水 from
scott.emp”,顯示結果時將'empno'變成'編號' 等等.
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章