SQL 運算符及模糊查詢,空字段的處理等等..doc_第1頁
SQL 運算符及模糊查詢,空字段的處理等等..doc_第2頁
SQL 運算符及模糊查詢,空字段的處理等等..doc_第3頁
SQL 運算符及模糊查詢,空字段的處理等等..doc_第4頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

語句所用表說明第二章:select 語句:where子句過濾1.And 和 or 運算符and運算符用于連接兩個布爾型表達式,當有所有表達式都為true時返回true,當有一個表達式返回為false時則返回false語法:Boolean_experssion and Boolean_experssion(1).利用圖書銷售表查詢圖書編號是1100010101和銷售數(shù)量為2的圖書單價Select b_price from Bookinfo where b_code=1100010101 and b_number = 2 or運算符也是用于連接兩個布爾型表達式,但是當表達式有一個為flase則返回false,語法:Boolean_experssion or Boolean_experssion(1).利用圖書信息表(Bookinfo)查詢圖書編號(b_code)是1100010101或者是1100010102的所有圖書信息Select * from Bookinfo where b_code=1100010101 or b_code = 1100010102注意:and優(yōu)先級大于or優(yōu)先級,要想改變優(yōu)先級的級別,則用小括號括起來2.比較用算符比較用算符:運算符說明=等于號大于號=大于等于號不大于!不小于或者!=不等于(1).查詢學(xué)生表中年齡為22學(xué)生的所有信息Select * from studenttable where studentage = 21(2).查詢學(xué)生表中學(xué)號大于6的學(xué)生所有信息Select * from studenttable where studentid6(3).查詢學(xué)生表中年齡小于21歲學(xué)生所有信息Select * from studenttable where studentage=23(5).查詢學(xué)生表中學(xué)號小于等于3的學(xué)生所有信息Select * from studenttable where studentid3(7). 查詢學(xué)生表中學(xué)號不小于3的學(xué)生所有信息Select * from studenttable where studentid!3(8). 查詢學(xué)生表中年齡不是24的學(xué)生所有信息Select *from studenttable where studentage !=24Select *from studenttable where studentage 243.in運算符 In運算符是根據(jù)給定的值進行查詢數(shù)據(jù)的,它的作用和or的作用是相同的, 但是用起來卻比or方便語法:test_expression not in(subquery|expressionn)(1) 查詢圖書銷售表中圖書編號(b_code)為01或者02的記錄Select *from Booksales where b_code in (1100010101,1100010102)(2) 查詢圖書銷售表中圖書單價為88的記錄Select * from Booksales where b_price in (66+22)(3) 查詢圖書銷售表中圖書單價為59.8的記錄Select * from Booksales where 59.8 in (b_price)(4) 查詢圖書銷售表中圖書編號不為01或者02的記錄Select * from Booksales where b_code not in (1100010101,1100010102)(5) 查詢圖書銷售表中圖書單價不為88或者58.8記錄Select * from Booksales where b_price not in (77+11,58+0.8)(6) 查詢圖書銷售表中圖書單價不為88的記錄Select * from Booksales where 88 not in (b_price)4.betweenand 和not between and 查找指定范圍數(shù)據(jù) Between and 是查詢指定范圍內(nèi)的數(shù)據(jù),語法:test_expression notbetween begin_expression and end_expression(1) 查詢學(xué)生表中年齡在20到23之間的學(xué)生姓名和學(xué)生年齡 Select studentname as 學(xué)生姓名,studentage 學(xué)生年齡 from studenttable where studentage between 20 and 23(2) 查詢學(xué)生表中年齡不在20到23之間的學(xué)生姓名和學(xué)生年齡Select studentname 學(xué)生姓名,studentage ”學(xué)生年齡” from studenttable where studentage not between 20 and 23(3) 查詢圖書銷售表中銷售日期在2010-07-14到2010-07-16之間的圖書編號 Select b_code as 圖書編號 from Booksales where b_date between 2010-07-14 and 2010-07-16(4) 查詢圖書銷售表中銷售日期不在2010-07-14到2010-07-06之間的圖書編號 Select b_code as 圖書編號 from Booksales where b_date not between 2010-07-07-14 and 2010-07-16注意:betweenand包含邊界值!5.like和通配符的使用Like和通配符是指對數(shù)據(jù)進行模糊查詢Like謂詞只能用于char,nchar,varchar,nvarchar,datetime,這些數(shù)據(jù)類型通配符說明%由0個或者多個字符組成的任意字符串_由1個字符組成的任意字符串a(chǎn),f a或者f的字符串,a-fa到f之接的任意單個字符a,f不是a或者f的字符串,a-f不是a-f之接的任意字符串(1)查詢學(xué)生表姓名以楊開頭的所有信息 Select * from studenttable where studentname like 楊%(2)查詢學(xué)生表姓名以盼結(jié)尾的所有信息Select * from studenttable where studentname like %盼(3) 查詢學(xué)生表姓名以楊開頭后面是任意一個字符的所有信息Select * from studenttable where studentname like 楊_(4) 查詢學(xué)生表姓名以博字結(jié)尾姓名是兩個字符的所有信息Select *from studenttbale where studentname like _博(5) 查詢學(xué)生表姓名以楊或者王開頭的兩個字符的所有信息Select * from studenttable where studentname like 楊,王_(6) 查詢學(xué)生表姓名以a或者c開頭的學(xué)生所有信息Select * from studenttable where studentname like a,c%(7)查詢學(xué)生表姓名以a到c之接任意字母結(jié)尾的學(xué)生信息Select * from studenttable where studentname like %a-c(8)查詢學(xué)生表姓名不是a或者b或者c開頭的所有學(xué)生信息Select * from studenttable where studentname like abc%(9) 查詢學(xué)生表姓名的開頭字母不再a到f之接的所有學(xué)生信息Select * from studenttable where studentname like a-f%(10) 利用escape將通配符轉(zhuǎn)化為實際字符Select * from studenttable where studentname like %胖%_ escape 胖6利用is null和not is null查詢?yōu)榭詹粸榭諗?shù)據(jù)及處理空值的函數(shù)isnull(),nullif()空值是未知的值,但空值不包括0,一個或多個空格組成的字符串,或者0長度的字符串,在實際應(yīng)用中,空值說明還沒有像數(shù)據(jù)庫中插入相應(yīng)的數(shù)據(jù)。A. isnull()函數(shù)的使用isnull()函數(shù)是將空值處理成指定的值(1) 將聯(lián)系方式表(contact)中的姓名為楊萬波的年齡替換是21Select id,names,sexs,isnull(ages,21) as ages, phone from contactB. nullif()函數(shù)的使用nullif()函數(shù)是將制定的值處理成空值(1) 將聯(lián)系方式表(contact)中的年齡為23歲的年齡用空值替換Select id,names,sexs,nullif(ages,23)as ages,phone from contactC. is null的使用(1) 查詢聯(lián)系方式表(contact)中的年齡為空的數(shù)據(jù)Select * from contact where ages is nullD. is not null的使用(1) 查詢聯(lián)系方式表(contact)中的年齡不為空的數(shù)據(jù)Select * from contact where ages is not null注:isnull()將值替換為空值并不是將原表中的數(shù)據(jù)更改為空值,而是將查詢出來的記錄集用中的數(shù)據(jù)用null替換!nullif()同isnull()一樣!如果要將查詢的結(jié)果集生成真的表則可以Select id,names,sexs,isnull(ages,21)as ages,phone into newcontact from contact7.利用謂詞any或者all查詢數(shù)據(jù)All:代表子查詢中的每一個,是且的關(guān)系,取兩個值A(chǔ)ny:代表子查詢中的每一個值,是或的關(guān)系,取一個值例如:Select * from Bookinfo where b_price all(select b_price from Bookinfo where b_author in (郭盼,雷雨年)就是查詢圖書價格大于作者是郭盼并且大于雷雨年的所有圖書信息,也就是大于最大值Select * from Bookinfo where b_price any(select b_price from Bookinfo where b_author in (郭盼,雷雨年)就是查詢圖書價格大于作者是郭盼或者大于雷雨年的所有圖書信息,也就是大于最小值(1)查詢圖書價格大于作者是雷雨年,郭盼的圖書信息Select *from Bookinfo where b_price all (select b_price from Bookinfo where b_author in (雷雨年,郭盼)(2)查詢圖書價格大于作者是雷雨年或者郭盼的圖書信息Select * from Bookinfo where b_priceany(select b_price from Bookinfo where b_author in (雷雨年,郭盼)8.exists的用法在一些情況下,只要子查詢返回一個真值或者假值,只考慮是否滿足謂詞條件,數(shù)據(jù)內(nèi)容本身并不重要的情況下,可用Exists來定義子查詢,如果子查詢返回一行或者多行,exists謂詞為真否則為假,要使exists謂詞有用,應(yīng)該在自查詢中建立查詢條件以匹配自查詢連接起來的兩個表中的值!語法:exists subquery例:(1)子查詢中查詢null返回結(jié)果為trueSelect * from Bookinfo where exitst(select null)(2) 查詢圖書

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論