日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLite | Where 子句

發布時間:2025/3/15 数据库 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLite | Where 子句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1. Where
    • 1.1 篩選記錄
    • 1.2 Using WHERE on Numbers
    • 1.3 邏輯判斷
    • 1.4 Using WHERE on Text
    • 1.5 Using Where on Booleans
    • 1.6 處理缺失值
  • 參考資料

1. Where

我們在上一篇中介紹了 Select 語句,接下來我們將使用 Where 子句,對數據進行篩選。

  • 使用Jupyter Notebook 運行 SQL 語句需安裝 ipython-sql

  • %sql 以及 %%sql 為在 Notebook 中運行 SQL 語句,在 SQLite 命令行或 SQLite Stiduo 中不需要 %sql 或 %%sql

載入 SQL 以及連接 SQLite:

%load_ext sql %sql sqlite:///DataBase/weather_stations.db 'Connected: @DataBase/weather_stations.db'

1.1 篩選記錄

本文將使用 weather_stations.db 數據庫,其中包含了 STATION_DATA 表。

首先查看 STATION_DATA 表中的數據:

%sql select * from station_data limit 0,10; -- 篩選前十行 * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
76644039537B199810172.71014.65.96.783.30None00000
176010C3C6D5200151855.7None7.34.369.10None00000
1256001451502007101433None6.92.539.70None00000
470160EF616A196772965.6None9.21.272.40.04None00000
8219301F8A7B195361872.81007.112.43.681.30None00000
478070D028D8198162773.4None7.93771.93None00000
719200C74611197825-4.4962.914.913.31.609.800000
477460737090196281472.31009.624.15.184.50None00000
598550C5C66E2006101572.9None14.21.7820None00000

1.2 Using WHERE on Numbers

圖1 SQLite 內置比較符

假如我們只對 STATION_DATA 表中 2010 年的數據感興趣,則使用 Where 子句是一個非常直接的方法。通過這個查詢,你可以只返回 year 中只等于 2010 的記錄:

%%sql select * from station_data where year == 2010 limit 0,3; -- 由于數據太多,我們只展示前 3 條記錄 * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
719160BAB9742010122-22.81014.2None10.2-18.509.400000
7668707C0938201032248871.24.41.550.80.11None11111
13462411CEA1201021746None3.42.646NoneNone00000

同樣的,你也可以使用 !< 或者 <> 來篩選信息:

%%sql select * from station_data where year != 2010 limit 0,3; -- 由于數據太多,我們只展示前 3 條記錄 * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
76644039537B199810172.71014.65.96.783.30None00000
176010C3C6D5200151855.7None7.34.369.10None00000

我們也可以使用 between 條件來篩選范圍:

%%sql select * from station_data where year between 2005 and 2010 limit 0,3; -- 由于數據太多,我們只展示前 3 條記錄 * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
1256001451502007101433None6.92.539.70None00000
598550C5C66E2006101572.9None14.21.7820None00000
941830229317200741966.5994.9None476.30None00000

1.3 邏輯判斷

圖2 SQLite 內置邏輯判斷符

一個 between 條件相當于表達了大于等于和小于等于,即 and 條件:

%%sql select * from station_data where year >=2005 and year <=2010 limit 0,3; -- 由于數據太多,我們只展示前 3 條記錄 * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
1256001451502007101433None6.92.539.70None00000
598550C5C66E2006101572.9None14.21.7820None00000
941830229317200741966.5994.9None476.30None00000

也可以通過 or 條件篩選記錄:

%%sql select * from station_data where Month==3 or Month==6 or Month==9 or Month==12 limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
8219301F8A7B195361872.81007.112.43.681.30None00000
478070D028D8198162773.4None7.93771.93None00000

這看起來有點麻煩,我們可以使用 in 來同樣篩選記錄:

%%sql select * from station_data where Month in (3,6,9,12) limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
8219301F8A7B195361872.81007.112.43.681.30None00000
478070D028D8198162773.4None7.93771.93None00000

或者這樣寫:

%%sql select * from station_data where Month % 3 == 0 limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
8219301F8A7B195361872.81007.112.43.681.30None00000
478070D028D8198162773.4None7.93771.93None00000

如果你不想要 3,6,9,12 月份的數據,你可以使用 not in

%%sql select * from station_data where Month not in (3,6,9,12) limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
76644039537B199810172.71014.65.96.783.30None00000
176010C3C6D5200151855.7None7.34.369.10None00000
1256001451502007101433None6.92.539.70None00000

1.4 Using WHERE on Text

圖2 SQLite 內置文字函數

我們已經舉了幾個將 where 用于數字字段的例子,對于文字字段,方法也是大同小異的,同樣可以使用 =, AND, OR 和 IN 。不同的是,對于文字,需要使用單引號:

%%sql select * from station_data where report_code == '513A63'; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
702223513A632010122-23.1None100.8-15.60None00000

注意 report_code 的格式是 text(而不是number),我們需要加上單引號 ‘513A63’ ,如果沒有單引號,SQL將會誤認為 513A63 是一列而不是一個值,這將會造成錯誤。

單引號適用于所有的文字操作,包括 IN 操作:

%%sql select * from station_data where report_code in ('513A63', '1F8A7B', 'EF616A') limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
470160EF616A196772965.6None9.21.272.40.04None00000
8219301F8A7B195361872.81007.112.43.681.30None00000
702223513A632010122-23.1None100.8-15.60None00000

在使用 WhereSelect 時,還有一些很有用的文字操作和函數,比如 length() 函數可以計算長度,來返回 report_code 不等于 6 的記錄:

%%sql select * from station_data where length(report_code) != 6;

另一個常見的操作符為一個通配符加上一個 like 表達,% 為任意長度的字符、_ 為任意單字符。如果你想要找到 report_code 中以 “A” 開頭的記錄,可以使用 ‘A%’:

%%sql select * from station_data where report_code like 'A%' limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
484750A38C90198862472.6None8.73.187.50None00000
985310A79DEC200773177.6None11.83.482.50None00000
724505A49553200542842.7None6.811.255.40.42None00000

如果你想要尋找以 “B” 開頭并且第三個字母是 “C” 的記錄,你可以使用下劃線(_)來作為第二個位置:

%%sql select * from station_data where report_code like 'B_C%' limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
999999B6C2DE19662838.8992.915.25.552.50None00000
60110B8CB27199712041.71008.313.119.144.70.04None00000
64080BECB5119828859None2.51165.50None00000

1.5 Using Where on Booleans

布爾值是 true 或 false 值。在某些數據庫中,使用 1、0 來代替 true 和 false,還有一些數據庫(如MySQL)允許你直接使用 true 和 false,比如:

%%sql select * from station_data where tornado == true and hail == true limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
72432020797919883433.1999.43.19.335.10.23None11111
7439202ABE7D199652157.6None5.87.5700None11111

SQLite 之前好像不支持直接使用 true 和 false ,只能使用 1 和 0,但經測試現在也可以了

在 SQLite 中也可以使用這種形式:

%%sql select * from station_data where tornado == 1 and hail == 1 limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
72432020797919883433.1999.43.19.335.10.23None11111
7439202ABE7D199652157.6None5.87.5700None11111

如果你正在查找為 true 的值,你甚至可以不使用 == 1 的表達,因為它的格式已經是布爾值了,因此你也可以這樣寫:

%%sql select * from station_data where tornado and hail limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
72432020797919883433.1999.43.19.335.10.23None11111
7439202ABE7D199652157.6None5.87.5700None11111

同樣,對于 false 也有兩種表達:

%%sql select * from station_data where tornado == 0 and hail == 1; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
%%sql select * from station_data where not tornado and hail; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado

1.6 處理缺失值

你可能發現了某一些列包含了缺失值(null values),比如 station_pressure 和 snow_depth。Null 無法使用 == 來查找,你需要使用 IS NULLIS NOT NULL 來浮現缺失值。因此,為了到所有沒有 snow_depth 數據的記錄,你可以使用以下查詢:

%%sql select * from station_data where snow_depth IS NULL limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
76644039537B199810172.71014.65.96.783.30None00000
176010C3C6D5200151855.7None7.34.369.10None00000

如果你想要將缺失值替換為一個確切的值,你可以使用 coalesce() 函數,如將降雨量為缺失值的數據替換為 0 ,同時篩選其中小于等于 0 的數據:

%%sql select * from station_data where coalesce(precipitation, 0) <= 0.5 limit 0,3; * sqlite:///DataBase/weather_stations.db Done. station_numberreport_codeyearmonthdaydew_pointstation_pressurevisibilitywind_speedtemperatureprecipitationsnow_depthfograinhailthundertornado
14308034DDA72002122133.8987.43.40.2360None11111
76644039537B199810172.71014.65.96.783.30None00000
176010C3C6D5200151855.7None7.34.369.10None00000

coalesce() 函數不僅可以用于 where 子句中, 還可以用于 select 語句中,如:

%%sql select report_code, coalesce(precipitation, 0) as rainfall from station_data limit 0,3; * sqlite:///DataBase/weather_stations.db Done. report_coderainfall
34DDA70
39537B0
C3C6D50

參考資料

[1] Thomas Nield.Getting Started with SQL[M].US: O’Reilly, 2016: 29-37

相關文章:

SQL | 目錄
SQLite | SQLite 與 Pandas 比較篇之一
SQLite | Select 語句
SQLite | Group by 與 Order by 子句
SQLite | CASE 子句
SQLite | Join 語句
SQLite | 數據庫設計與 Creat Table 語句
SQLite | Insert、Delete、Updata 與 Drop 語句

總結

以上是生活随笔為你收集整理的SQLite | Where 子句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。