嵌入式數(shù)據(jù)庫(kù)sqlite3【進(jìn)階篇】-子句和函數(shù)的使用,小白一文入門

<p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-e06b9a4ac899dae7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>sqlite</p><p>在《嵌入式數(shù)據(jù)庫(kù)sqlite3命令操作基礎(chǔ)篇-增刪改查,小白一文入門》一文中講解了如何實(shí)現(xiàn)sqlite3的基本操作增刪改查,本文介紹一些其他復(fù)雜一點(diǎn)的操作。比如where、order by、having、like、函數(shù)等用法。</p><h1>數(shù)據(jù)庫(kù)準(zhǔn)備</h1><p>新建數(shù)據(jù)庫(kù),company.db。
設(shè)計(jì)一個(gè)表格employee,內(nèi)容如下:</p><p>idnameagedepsalary1馬云21market60002馬化騰22tech70003李彥宏23trs86004張朝陽(yáng)24trs60005羅永浩26tech89006王欣25market4000</p><p class="image-package">根據(jù)上述表格,我們首先確定主鍵是id。
創(chuàng)建表格命令如下:</p><pre>CREATE?TABLE?employee(id?integer?primary?key,?name?text,age?integer?,?dep?text,?salary?integer);</pre><pre>insert?into?employee?values?(1,'馬云',21,'market',6000);insert?into?employee?values?(2,'馬化騰',22,'tech',7000);insert?into?employee?values?(3,'李彥宏',23,'trs',8000);insert?into?employee?values?(4,'張朝陽(yáng)',24,'trs',6000);insert?into?employee?values?(5,'羅永浩',26,'tech',8900);insert?into?employee?values?(6,'王欣',25,'market',4000);insert?into?employee?values?(7,'一口',18,'market',4000);</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-fd677828ed5392ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-876bb7b744c23836.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><h1>order子句</h1><p>我們可以使用order子句實(shí)現(xiàn)對(duì)記錄的排序:</p><pre>select??from?employee?order?by?age;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-4a80f72deec4881e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><h1>Where 子句與邏輯運(yùn)算符</h1><p>SQLite的 WHERE 子句用于指定從一個(gè)表或多個(gè)表中獲取數(shù)據(jù)的條件。
如果滿足給定的條件,即為真(true)時(shí),則從表中返回特定的值。您可以使用 WHERE 子句來(lái)過(guò)濾記錄,只獲取需要的記錄。
WHERE 子句不僅可用在 SELECT 語(yǔ)句中,它也可用在 UPDATE、DELETE 語(yǔ)句中,等等,這些我們將在隨后的章節(jié)中學(xué)習(xí)到。</p><p/><p>SQLite 的帶有 WHERE 子句的 SELECT 語(yǔ)句的基本語(yǔ)法如下:</p><pre>SELECT?column1,?column2,?columnN?FROM?table_name?WHERE?[condition]</pre><p/><p>您還可以使用比較或邏輯運(yùn)算符指定條件,比如 >、<、=、>=,<= ,LIKE、NOT,等等。</p><p>下面的實(shí)例演示了 SQLite 邏輯運(yùn)算符的用法。</p><p><strong>下面的 SELECT 語(yǔ)句列出了 AGE 大于等于 25 且工資大于等于 65000.00 的所有記錄:</strong></p><pre>?SELECT?
?FROM?EMPLOYEE?WHERE?AGE?>=?25?AND?SALARY?>=?6500;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-8e7429fc5bd19457.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>下面的 SELECT 語(yǔ)句列出了 AGE 大于等于 25 或工資大于等于 65000.00 的所有記錄:</strong></p><p>SELECT * FROM EMPLOYEE WHERE AGE >= 25 OR SALARY >= 65000;</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-5453ac0b11d53ddd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>下面的 SELECT 語(yǔ)句列出了 AGE 不為 NULL 的所有記錄,結(jié)果顯示所有的記錄,意味著沒(méi)有一個(gè)記錄的 AGE 等于 NULL:</strong></p><pre>SELECT??FROM?EMPLOYEE?WHERE?AGE?IS?NOT?NULL;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-ed97412d9465bde9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>where子句還可以配合like子句一起使用。<strong>下面的 SELECT 語(yǔ)句列出了 NAME 以 'Ki' 開始的所有記錄,'Ki' 之后的字符不做限制:</strong></p><pre>SELECT??FROM?EMPLOYEE?WHERE?NAME?LIKE?'馬%';</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-2c914c4e398cbba2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/><strong>下面的 SELECT 語(yǔ)句列出了 AGE 的值為 22 或 25 的所有記錄:</strong></p><pre>SELECT??FROM?EMPLOYEE?WHERE?AGE?IN?(?22,?25?);</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-608f6bc4dc75db9f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>下面的 SELECT 語(yǔ)句列出了 AGE 的值既不是 25 也不是 27 的所有記錄:</strong></p><pre>SELECT??FROM?EMPLOYEE?WHERE?AGE?NOT?IN?(?22,?25?);</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-76e712b7204006e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>下面的 SELECT 語(yǔ)句列出了 AGE 的值在 22 與 25 之間的所有記錄:</strong></p><pre>SELECT??FROM?EMPLOYEE?WHERE?AGE?BETWEEN?22?AND?25;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-ea686a17bfb23998.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><h1>group by子句</h1><p>GROUP BY 語(yǔ)句用于結(jié)合聚合函數(shù),根據(jù)一個(gè)或多個(gè)列對(duì)結(jié)果集進(jìn)行分組。
舉例:</p><p><strong>統(tǒng)計(jì)整個(gè)公司工資總和:</strong></p><pre>select?sum(salary)?from?employee;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-05bf08712d03809a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>統(tǒng)計(jì)每個(gè)部門的工資總和:</strong></p><pre>select?dep,?sum(salary)?from?employee?group?by?dep;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-f7b9ed442248d55a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/><strong>統(tǒng)計(jì)各部門的工資總和并且要求id值大于3</strong></p><pre>select?dep,?sum(salary)?from?employee?where?id>3?group?by?dep;????where子句要放在group?by的前面。</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-3c98d982580a6610.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p/><p>having子句是group by的條件子句,where子句先發(fā)生,然后才是having 子句執(zhí)行。
HAVING子句中能夠使用三種要素:常數(shù),聚合函數(shù),GROUP BY子句中指定的列名(聚合建),
用having就一定要和group by連用, 用group by不一有having(它只是一個(gè)篩選條件用的)</p><p/><p><strong>統(tǒng)計(jì)各部門的工資總和并且要求id值大于3</strong></p><pre>select?dep,?sum(salary)?from?employee?where?id>3?group?by?dep?having?sum(salary)>5000;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-0b099b1cbc2ae2b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/><strong>查找重復(fù)記錄的方法</strong></p><p>我們先插入依據(jù)名字相同的記錄。</p><pre>insert?into?employee?values?(8,'一口',19,'market',5000);select?id,name?from?employee?group?by?name?having?count(
)?>?1;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-a6896b40498c771a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>可以看到,結(jié)果顯示了相同名字的重復(fù)記錄。</p><p><strong>顯示名字相同的多項(xiàng)</strong></p><pre>select?id,name,dep,salary??from?employee?group?by?name??having?count()?>?1;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-942784fdf0fe1cb6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/><strong>顯示table中所有的記錄</strong></p><pre>select?count()?from?employee;</pre><p><strong>顯示所有記錄的個(gè)數(shù)</strong></p><pre>select?dep,avg(salary)?from?employee?group?by?dep;</pre><p><strong>顯示dep下每一組的平均值</strong></p><pre>select??from?employee?where?id?>?3?intersect?select??from?employee?where?id?<?9;</pre><p><strong>顯示id > 3 && id < 9 的所有記錄:即4 - 8 的記錄</strong></p><pre>select??from?employee?where?id?>?3?union?all?select??from?employee?where?id?<?9;</pre><p><strong>顯示所有的大于3并且小于9的,并集(如果有相同的,會(huì)重復(fù)顯示)</strong></p><pre>select??from?studentnew?where?id?>?3?union?all?select??from?studentnew?where?id?<?9;</pre><p><strong>顯示大于9的記錄</strong></p><pre>select??from?employee?where?id?>?3?union?all?select??from?studentnew?where?id?<?6;</pre><p><strong>顯示大于6的記錄,(與上一個(gè)進(jìn)行比較)</strong></p><pre>select?from?employee?where?salary=?(select?salary?from?employee?order?by?salary?desc?limit?1);select?from?employee?where?salary=?(select?max(salary)?from?employee?);</pre><p><strong>顯示最高工資的所有員工的記錄</strong></p><pre>select?name,max(salary)?from?employee;</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-60c835179f332dd0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>顯示table中名字不相同的員工;</strong></p><pre>select?distinct?name?from?employee;</pre><p><strong>顯示所有員工的名字;</strong></p><pre>?select?all?name?from?employee;</pre><h1>函數(shù)</h1><p>SQLite 有許多內(nèi)置函數(shù)用于處理字符串或數(shù)字?jǐn)?shù)據(jù)。下面列出了一些有用的 SQLite 內(nèi)置函數(shù),且所有函數(shù)都是大小寫不敏感,這意味著您可以使用這些函數(shù)的小寫形式或大寫形式或混合形式。
欲了解更多函數(shù)的說(shuō)明,可以參考 SQLite 的官方文檔,下面列舉常用的一些函數(shù)</p><p>序號(hào)函數(shù)& 描述1.COUNTSQLite COUNT 聚集函數(shù)是用來(lái)計(jì)算一個(gè)數(shù)據(jù)庫(kù)表中的行數(shù)。2.MAXSQLite MAX 聚合函數(shù)允許我們選擇某列的最大值。3.MINSQLite MIN 聚合函數(shù)允許我們選擇某列的最小值。4.AVGSQLite AVG 聚合函數(shù)計(jì)算某列的平均值。5.SUMSQLite SUM 聚合函數(shù)允許為一個(gè)數(shù)值列計(jì)算總和。6.RANDOMSQLite RANDOM 函數(shù)返回一個(gè)介于 -9223372036854775808 和 +9223372036854775807 之間的偽隨機(jī)整數(shù)。7.ABSSQLite ABS 函數(shù)返回?cái)?shù)值參數(shù)的絕對(duì)值。8.UPPERSQLite UPPER 函數(shù)把字符串轉(zhuǎn)換為大寫字母。9.LOWERSQLite LOWER 函數(shù)把字符串轉(zhuǎn)換為小寫字母。10.LENGTHSQLite LENGTH 函數(shù)返回字符串的長(zhǎng)度。11.sqlite_versionSQLite sqlite_version 函數(shù)返回 SQLite 庫(kù)的版本。</p><p/><p>SQLite COUNT 聚集函數(shù)是用來(lái)計(jì)算一個(gè)數(shù)據(jù)庫(kù)表中的行數(shù)。下面是實(shí)例:</p><pre>SELECT?count()?FROM?EMPLOYEE?;</pre><p>執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-0b196e45d58a4a1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p/><p>SQLite MAX 聚合函數(shù)允許我們選擇某列的最大值。下面是實(shí)例:</p><pre>SELECT?max(salary)?FROM?EMPLOYEE?;</pre><p class="image-package">執(zhí)行結(jié)果:<img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-d93a3666af887c4f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>SQLite MIN 聚合函數(shù)允許我們選擇某列的最小值。下面是實(shí)例:</p><pre>SELECT?min(salary)?FROM?EMPLOYEE?;</pre><p>執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-708dd1a3dbee3ea6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p/><p>SQLite AVG 聚合函數(shù)計(jì)算某列的平均值。下面是實(shí)例:</p><pre>SELECT?avg(salary)?FROM?EMPLOYEE?;</pre><p class="image-package">執(zhí)行結(jié)果:<img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-91e86bc2cab71606.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>SQLite SUM 聚合函數(shù)允許為一個(gè)數(shù)值列計(jì)算總和。下面是實(shí)例:</p><pre>SELECT?sum(salary)?FROM?EMPLOYEE?;</pre><p>執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-5b24b2682ef00abf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p/><p>SQLite RANDOM 函數(shù)返回一個(gè)介于 -9223372036854775808 和 +9223372036854775807 之間的偽隨機(jī)整數(shù)。下面是實(shí)例:</p><pre>SELECT?random()?AS?Random;</pre><p>執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-977b0eda1640a191.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p/><p>SQLite ABS 函數(shù)返回?cái)?shù)值參數(shù)的絕對(duì)值。下面是實(shí)例:</p><pre>SELECT?abs(5),?abs(-15),?abs(NULL),?abs(0),?abs("ABC");</pre><p>執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-efef320dea86ccf6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p/><p>SQLite UPPER 函數(shù)把字符串轉(zhuǎn)換為大寫字母。下面是實(shí)例:</p><pre>insert?into?employee?values?(9,'yikoulinux',22,'market',8000);SELECT?upper(name)?FROM?EMPLOYEE?;</pre><p class="image-package">執(zhí)行結(jié)果:<img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-32d1b0b61881b9c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>SQLite LOWER 函數(shù)把字符串轉(zhuǎn)換為小寫字母。下面是實(shí)例:</p><pre>SELECT?lower(name)?FROM?EMPLOYEE?;</pre><p class="image-package">執(zhí)行結(jié)果:<img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-89acc155375cbcbf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>SQLite LENGTH 函數(shù)返回字符串的長(zhǎng)度。下面是實(shí)例:</p><pre>SELECT?name,?length(name)?FROM?EMPLOYEE?;</pre><p class="image-package">執(zhí)行結(jié)果:<img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-31e783be46539dc1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>SQLite sqlite_version 函數(shù)返回 SQLite 庫(kù)的版本。下面是實(shí)例:</p><pre>SELECT?sqlite_version()?AS?'SQLite?Version';</pre><p class="image-package">執(zhí)行結(jié)果:<img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-4c380f40492e971a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p/><p>datetime() 產(chǎn)生日期和時(shí)間 ? ?無(wú)參數(shù)表示獲得當(dāng)前時(shí)間和日期</p><pre>select?datetime();</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-3d5685953090136e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>在這里插入圖片描述</p><p><strong>有字符串參數(shù)則把字符串轉(zhuǎn)換成日期</strong></p><pre>select?datetime('2012-01-07?12:01:30');</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-7cc8cd7591131fd8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>2012-01-07 12:01:30</p><pre>select?date('2019-09-09','+1?day','+1?year');</pre><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-1e1d3f5795139beb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/>2013-01-09</p><pre>select?datetime('2019-09-09?00:20:00','+1?hour','-12?minute');</pre><p>2019-09-09 01:08:00</p><pre>select?datetime('now','start?of?year');</pre><p>2020-01-01 00:00:00</p><pre>select?datetime('now','start?of?month');</pre><p>2020-09-01 00:00:00</p><pre>select?datetime('now','start?of?day');</pre><p class="image-package">2020-09-13 00:00:00</p><pre>select?datetime('now','localtime');</pre><p>結(jié)果:2020-09-12 20:26:35</p><p/><p>date()用于產(chǎn)生日期</p><pre>select?date('2019-09-09?12:01:30');</pre><p>2019-09-09</p><pre>select?date('now','start?of?year');</pre><p>2020-01-01</p><pre>select?date('2019-09-09','+1?month');</pre><p>2019-10-09</p><p/><p class="image-package">time() 用于產(chǎn)生時(shí)間。</p><pre>select?time();</pre><p>03:28:49</p><pre>select?time('23:18:59');</pre><p>23:18:59</p><pre>select?time('23:18:59','start?of?day');</pre><p>00:00:00</p><p>在時(shí)間/日期函數(shù)里可以使用如下格式的字符串作為參數(shù):</p><ul><li><p>YYYY-MM-DD</p></li><li><p>YYYY-MM-DD HH:MM</p></li><li><p>YYYY-MM-DD HH:MM:SS</p></li><li><p>YYYY-MM-DD HH:MM:SS.SSS</p></li><li><p>HH:MM</p></li><li><p>HH:MM:SS</p></li><li><p>HH:MM:SS.SSS</p></li><li><p>now</p></li></ul><p>其中now是產(chǎn)生現(xiàn)在的時(shí)間。</p><p>日期不能正確比較大小,會(huì)按字符串比較,日期默認(rèn)格式 dd-mm-yyyy</p><p/><p>strftime() 對(duì)以上三個(gè)函數(shù)產(chǎn)生的日期和時(shí)間進(jìn)行格式化。</p><p>strftime()函數(shù)可以把YYYY-MM-DD HH:MM:SS格式的日期字符串轉(zhuǎn)換成其它形式的字符串。
strftime(格式, 日期/時(shí)間, 修正符, 修正符, …)</p><p>它可以用以下的符號(hào)對(duì)日期和時(shí)間進(jìn)行格式化:</p><pre>%d?在該月中的第幾天,?01-31?%f?小數(shù)形式的秒,SS.SSS?%H?小時(shí),?00-23?%j?算出某一天是該年的第幾天,001-366?%m?月份,00-12?%M?分鐘,?00-59?%s?從1970年1月1日到現(xiàn)在的秒數(shù)?%S?秒,?00-59?%w?星期,?0-6?(0是星期天)?%W?算出某一天屬于該年的第幾周,?01-53?%Y?年,?YYYY?%%?百分號(hào)</pre><pre>select?strftime('%Y.%m.%d?%H:%M:%S','now');</pre><p class="image-package">2020.09.13 03:32:49</p><pre>select?strftime('%Y.%m.%d?%H:%M:%S','now','localtime');</pre><p>2020.09.12 20:33:24</p><h1>SELECT LIKE 用法</h1><p>在SQL結(jié)構(gòu)化查詢語(yǔ)言中,LIKE語(yǔ)句有著至關(guān)重要的作用。</p><p/><p>LIKE語(yǔ)句的語(yǔ)法格式是:</p><pre>select?
?from?表名?where?字段名?like?對(duì)應(yīng)值(子串),</pre><p>它主要是針對(duì)字符型字段的,它的作用是在一個(gè)字符型字段列中檢索包含對(duì)應(yīng)子串的。</p><ul><li><p>A):% 包含零個(gè)或多個(gè)字符的任意字符串:</p></li></ul><ol><li><p>LIKE'Mc%' 將搜索以字母 Mc 開頭的所有字符串(如 McBadden)。</p></li><li><p>LIKE'%inger' 將搜索以字母 inger 結(jié)尾的所有字符串(如 Ringer、Stringer)。</p></li><li><p>LIKE'%en%' 將搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。</p></li></ol><ul><li><p>B:_(下劃線) 任何單個(gè)字符:LIKE'_heryl' 將搜索以字母 heryl 結(jié)尾的所有六個(gè)字母的名稱(如 Cheryl、Sheryl)。</p></li><li><p>C:[ ] 指定范圍 ([a-f]) 或集合 ([abcdef]) 中的任何單個(gè)字符:</p></li></ul><ol><li><p>LIKE'[CK]ars[eo]n' 將搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。</p></li><li><p>LIKE'[M-Z]inger' 將搜索以字符串 inger 結(jié)尾、以從 M 到 Z 的任何單個(gè)字母開頭的所有名稱(如 Ringer)。</p></li></ol><ul><li><p>D:[^] 不屬于指定范圍 ([a-f]) 或集合 ([abcdef]) 的任何單個(gè)字符:LIKE'M[^c]%' 將搜索以字母 M 開頭,并且第二個(gè)字母不是 c 的所有名稱(如MacFeather)。</p></li><li><p>E:* 它同于DOS命令中的通配符,代表多個(gè)字符:cc代表cc,cBc,cbc,cabdfec等多個(gè)字符。</p></li><li><p>F:?同于DOS命令中的?通配符,代表單個(gè)字符 :b?b代表brb,bFb等</p></li><li><p>G:# 大致同上,不同的是代只能代表單個(gè)數(shù)字。k#k代表k1k,k8k,k0k 。</p></li><li><p>F:[!] 排除 它只代表單個(gè)字符</p></li></ul><p/><p>下面我們來(lái)舉例說(shuō)明一下:</p><ul><li><p>例1,查詢name字段中包含有“明”字的。</p></li></ul><pre>select??from?table1?where?name?like?'%明%'</pre><ul><li><p>例2,查詢name字段中以“李”字開頭。</p></li></ul><pre>select??from?table1?where?name?like?'李'</pre><ul><li><p>例3,查詢name字段中含有數(shù)字的。</p></li></ul><pre>select??from?table1?where?name?like?'%[0-9]%'</pre><ul><li><p>例4,查詢name字段中含有小寫字母的。</p></li></ul><pre>select??from?table1?where?name?like?'%[a-z]%'</pre><ul><li><p>例5,查詢name字段中不含有數(shù)字的。</p></li></ul><pre>select??from?table1?where?name?like?'%[!0-9]%'</pre><p>以上例子能列出什么值來(lái)顯而易見。但在這里,我們著重要說(shuō)明的是通配符“”與“%”的區(qū)別。
很多朋友會(huì)問(wèn),為什么我在以上查詢時(shí)有個(gè)別的表示所有字符的時(shí)候用"%"而不用“”?先看看下面的例子能分別出現(xiàn)什么結(jié)果:</p><pre>select?
?from?table1?where?name?like?''select??from?table1?where?name?like?'%明%'</pre><p>大家會(huì)看到,前一條語(yǔ)句列出來(lái)的是所有的記錄,而后一條記錄列出來(lái)的是name字段中含有“明”的記錄,所以說(shuō),當(dāng)我們作字符型字段包含一個(gè)子串的查詢時(shí)最好采用“%”而不用“”,用“”的時(shí)候只在開頭或者只在結(jié)尾時(shí),而不能兩端全由“”代替任意字符的情況下。</p><p>SQLITE數(shù)據(jù)庫(kù)的操作,暫告一段落,雖然是輕量級(jí)數(shù)據(jù)庫(kù),但是數(shù)據(jù)庫(kù)的操作基本思想是一致的,基本上是學(xué)一通百,下一章,我們來(lái)學(xué)習(xí)如何通過(guò)c語(yǔ)言程序來(lái)操作數(shù)據(jù)庫(kù)。</p><p>更多Linux相關(guān)的知識(shí),請(qǐng)關(guān)注公眾號(hào) 有Linux</p><p>
</p>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容