Oracle資料庫 —— SQL之select語句(2) where子句和資料排序
6 select語句
6.2 where子句(條件子句)
1、where的作用,是對錶中行資料進行限制,符合where條件資料就被選中,不符合where條件資料就被過濾掉。
select id,salary from s_emp;
select id,salary from s_emp where 1=1;
select id,salary from s_emp where 1=2;
2、number型別資料判斷
查詢工資大於1400的員工的id和first_name,salary
select id,first_name,salary from s_emp where salary>1400;
查詢工資小於1400的員工的id和first_name,salary
select id,first_name,salary from s_emp where salary<1400;
查詢工資等於1400的員工的id
select id,first_name,salary from s_emp where salary=1400;
3、字串型別的數字
查詢first_name叫carmen的員工的id,first_name,salary
select id,first_name,salary from s_emp where first_name=’Carmen’;
/*在sql中,字串的值是要嚴格區分大小寫的*/
4、where條件後的運算子號
> < = >= <= !=
5、sql提供了一些可以用在where後的運算子
a、where 欄位 between a and b
表達一個閉區間[a,b]
列出工資在[1000,2000]之間的員工的id,first_name,salary
select id,first_name,salary from s_emp where salary between 1000 and 2000;
/*數字寫反了,就查不到,邏輯錯誤*/
b、where 欄位 in (值的列表)
列出部門編號是41或者是50的部門的員工的id,first_name,dept_id
select id,first_name,dept_id from s_emp where dept_id in (41,50);
出現概率高的資料放前面,可提高查詢效率。
c、模糊查詢
where 欄位 like ‘通配串’
sql中的萬用字元:
% — 0-n個任意字元
_ — 1個任意字元
where 欄位 like ‘王%’;
where 欄位 like ‘%濤%’;
列出s_emp中first_name所有帶a的
select first_name from s_emp where first_name like ‘%a%’;
列出s_emp表first_name所有第二個字元是a的
select first_name from s_emp where first_name like ‘_a%’;
desc user_tables;
select table_name from user_tables;
查詢user_tables中所有的S_開頭的表
select table_name from user_tables where table_name like ‘S_%’;
/*需要進行轉義*/
select table_name from user_tables where table_name like ‘S\_%’ escape ‘\’;
select table_name from user_tables where table_name like ‘S\_\_%’ escape ‘\’;(兩個下劃線開頭的)
/*建立S__開頭的表*/
create table s__testgg(id number);
d、where 欄位 is null;
判斷一個欄位的值是不是NULL
找出manager_id是NULL的員工的id first_name manager_id
select id,first_name,manager_id from s_emp where manager_id is null;
查詢提成是10的員工的id first_name salary
select id,first_name,salary from s_emp where commission_pct=10;
查詢提成不是10的員工的id first_name salary
select id,first_name,salary from s_emp where commission_pct!=10;
查詢提成是NULL的員工的id first_name salary(提成為空的既不在等於10裡面也不在不等於10裡面)
select id,first_name,salary from s_emp where commission_pct is null;
6、邏輯運算子
and
實現工資在[1000,2000]之間的員工的id first_name salary
select id,first_name,salary from s_emp where salary>=1000 and salary<=2000;
實現工資(1000,2000)之間的員工的
select id,first_name,salary from s_emp where salary>1000 and salary<2000;
or
求部門編號是41或者是50的部門的員工的id first_name dept_id
select id,first_name,dept_id from s_emp where dept_id=41 or dept_id=50;
not
> <=
< >=
= != <> ^=
between a and b — not between a and b
in — not in(注意NULL的值)
like — not like
is null — is not null
/*提成不是NULL員工的id first_name commission_pct*/
select id,first_name,salary from s_emp where commission_pct is not null;
6.3 資料排序
order by 排序標準 排序方式;
order by 出現在sql語句最後
排序標準:排序的欄位
排序的方式:升序 — 預設的排序 asc 自然順序 字典順序
降序 – desc 反自然順序 反字典順序
按照工資排序,列出id first_name salary
select id,first_name,salary from s_emp order by salary;
按照工資降序排列
select id,first_name,salary from s_emp order by salary desc;
按照manager_id排序,列出id first_name manager_id
select id,first_name,manager_id from s_emp order by manager_id desc;
/*空值(NULL)在排序中當作最大值來處理*/
/*當按著某個欄位排序時,如果值相同了,我們可以啟用第二排序欄位*/
select id,first_name,salary from s_emp order by salary desc,id desc;
order by 排序標準 排序方式,排序標準2 排序方式;
相關推薦
Oracle資料庫 —— SQL之select語句(2) where子句和資料排序
6 select語句 6.2 where子句(條件子句) 1、where的作用,是對錶中行資料進行限制,符合where條件資料就被選中,不符合where條件資料就被過濾掉。 select id,salary from s_emp; select
《SQL資料庫視訊》之玩轉SELECT語句(一)
敲機房收費系統的時候,感覺我的SELECT語句總是千篇一律,txtSQL=”select* from talbel _source where search_condition ”這怎麼可以,把整個系統敲完也沒有把最基本的select語句格式用到自己的程式
資料庫之redis篇(2)—— redis配置檔案,常用命令,效能測試工具
redis配置 如果你是找網上的其他教程來完成以上操作的話,相信你見過有的啟動命令是這樣的: 啟動命令帶了這個引數:redis.windows.conf,由於我測試環境是windows平臺,所以是這個,有的是redis.conf。顧名思義,redis.conf就是配置檔案,然後啟動時加
mybatis中動態sql語句(2)
最近在專案中遇到這樣的需求:根據不同的條件修改和插入sql語句時,要寫很多的sql這樣效率有些低,這時有一個很好的解決方法,分享一下: 控制同一張表,但傳入的引數不固定,操作表的欄位不固定,就要用到mybatis動態插入。.xml如下 <insert id="ins
『ORACLE』 PLSQL條件控制語句(11g)
lin proc lar plsql 判斷語句 條件 num success end 簡單條件判斷語句 SQL> declare 2 myage number := 10; 3 begin 4 if myage < 11 then 5 dbms
(7)javascript的程序控制結構及語句------(2)循環控制語句、跳轉語句、對話框
對話 ima bsp .cn while語句 prompt 彈出 asc div 一、循環控制語句 循環語句主要就是在滿足條件的情況下反復執行某一個操作,循環控制語句主要包括while語句、do...while語句 和for語句。 1、While
Python+selenium之測試報告(2)
def window win get hot 過程 https 書寫 screen 1 # -*- coding: utf-8 -*- 2 import HTMLTestReport 3 import HTMLTestRunner 4 import os 5 i
崔華基於oracle的SQL優化讀書筆記(一)如何得到真實的執行計劃
hash mes getting binary oracl only 中文 fun roc ---恢復內容開始--- 得到目標SQL的執行計劃,大致有以下四種方式: 1.explain plan 命令 2.DBMS_XPLAN包 3.SQLPLUS中的autotrace開關
Oracle數據庫的刪除(2)
nbsp 新的 ces art oracl eventlog war rod files 在Windows中徹底刪除原先的Oracle,然後再重新安裝Oracle數據庫。具體步驟如下: 1、 開始->設置->控制面板->管理工具->服務,停止所有Oracle服務
Scala 語言學習之語言基礎(2)
err 實參 col res pre tom mom argument final 註: --------scala 中的任何數據都是對象 --------Scala 可以對數據類型進行自動推導,所以定義變量時可省去數據類型==> 數據類型
Linux學習之路-Nginx(2)安裝及配置文件篇【23】---20180210
Nginx編譯 Nginx yum安裝 主配置文件分析 Core functionality分 一、Nginx的安裝方法及配置介紹1、yum安裝官方: http://nginx.org/packages/centos/7/x86_64/RPMSFedora-EPEL: http
C之分支語句(六)
C語言 if 選擇語句 switch 選擇語句 我們在日常生活中難免會遇到要做選擇的情況,那麽在 C 語言中也有做選擇的情況。在這種情況下,我們的代碼又該怎樣寫呢?這時就可以用到我們的分支語句了。 總體來說,我們在 C 語言中可以經常看到兩種分支語句:第一種 if 分支語句,第二
PL/SQL批處理語句(一)BULK COLLECT
數據 使用 for循環 差異 code 基於 name 從表 允許 我們知道PL/SQL程序中運行SQL語句是存在開銷的,因為SQL語句是要提交給SQL引擎處理,這種在PL/SQL引擎和SQL引擎之間的控制轉移叫做上下文卻換,每次卻換時,都有額外的開銷。然而,FORALL和
《Linux學習並不難》Linux字符界面(2):關閉和重啟Linux系統之shutdown命令
Linux shutdown 關機 3.2 《Linux學習並不難》Linux字符界面(2):關閉和重啟Linux系統之shutdown命令shutdown命令可以安全地關閉或重啟Linux系統,有些用戶會使用直接斷掉電源的方式來關閉計算機,這是十分危險的。Linux系統與Windows不同,其
Day2----Python學習之路筆記(2)
cell 數據類型的轉換 編碼格式 python3 () shel 不能 索引 png 學習路線: Day1 Day2 Day3 Day4 Day5 ...待續 一、簡單回顧一下昨天的內容 1. 昨天了解到了一些編碼的知識 1.1
http協議之版本差異(2)
大量 XML cti 技術 類型 版本 bsp 沒有 域名 —————————————HTTP1.0/HTTP1.1—————————————— 建立連接方面 HTTP/1.0 每次請求都需要建立新的TCP連接,連接不能復用。HTTP/1.1 新的請求可以在上次請求
常用數據庫操作語句(2)
數據 != 排序 編程 就是 int str 表結構 author 查看表結構desc test; 顯示表列定義show columns from test; 顯示表的索引show index from test; 插入表數據INSERT INTO book (book_n
scala成長之路(2)對象和類
ascii int 紅色 部分 類名 基類 req 字面量 標識符 scala提供了一種特殊的定義單例的方法:object關鍵字 scala> object Shabi{ | val age = 0 | val name = "shabi"
循環語句(2)和列表
函數 包含 格式 con closed 關系 控制 list() range 循環語句2 for 語句 作用: 用來遍歷(所有的事情只經歷一遍)叠代對象的數據元素 講解:火車發現朋友發朋友圈同一兩火車,去找朋友,火車車廂的,把所有的車廂找
C語言學習筆記(5)—— 語句(2)
在介紹過順序結構和選擇結構以後,我們能通過c語言做的事依舊很少,比如換硬幣問題。將一元的人民幣換成一分,兩分,五分的硬幣共50枚。如何解決這樣的問題呢?我們可以把它化解成一個求解線性方程組的問題。 可以設需要一分的硬幣x枚,兩分的硬幣y枚,五分的硬幣z枚。於是,我們可以得到一個方程組,像這樣:x+y