My SQL 使用WHERE過濾資料

2021-08-05 19:59:17 字數 1905 閱讀 8791

where關鍵字相當於excel的篩選,不同的是where只能實現完全匹配,不能實現部分匹配,相同的是,where不區分大小寫,即如果判定where prod_id='fb',同樣可以篩選出fb的行。

在同時使用order by和where時,應該將order by位於where之後。

在這裡我們假設有一張表,名為products,內容如下:

常用的where命令和舉例如下:

單一條件過濾(where後面跟著乙個對列的過濾條件;如果是字串比較,需要加引號,數值比較則不用;)

常用的比較符號有:「<>」表示不等於,「!=」表示不等於,「」表示大於,「>=」表示大於等於,「<=」表示小於等於;

select prod_name,prod_price

from products

where prod_price-2.5;

結果如下:

範圍值過濾(between a and b,表示範圍在[a,b]之間的值,包括a和b本身)

select prod_name,prod_price

from products

where prod_price between 2 and 10;

結果如下:

空值檢測(過濾null值)

select prod_name,prod_price

from products

where prod_price is null;

結果顯示prod_price為空的行。

多條件過濾(可以用來組合的操作符有and表示與,or表示或;如果條件很複雜,可以使用圓括號來區分次序)

select prod_name,vend_id,prod_price

from products

where vend_id=1003 and prod_price>10;

結果如下:

列表過濾(in後面跟的是乙個列表,並不表示範圍;in(a,b)表示篩選a和b)

select prod_name,vend_id

from products

where vend_id in (1001,1003);

結果如下:

否定條件(not後面跟的所有操作符將被否定)

select prod_name,vend_id

from products

where vend_id not in(1001,1003);

結果如下:

MySql 使用where子句過濾資料

示例使用的資料表在上乙個部落格中建立的 示例如下 查詢 等於10.5的行 select from commodity where price 10.5 輸出 1 1001 牙刷 10.5 上個示例中,我們使用到了 這個操作符,下面列出所有條件操作符 where操作符 等於 不等於 不等於 小於 大於...

MySql 過濾資料where語句

1 使用where子句 只檢索所需要的資料則需要制定搜尋條件,搜尋條件也叫作過濾條件。例如 select 列名 from 表名 where 列名 value 只返回該列中值為value的行 where子句的位置 在同時使用order by 和where子句時,應該讓order by 位於where之...

SQL 過濾資料(使用WHERE子句)

只檢索所需要資料需要指定搜尋條件,搜尋條件也稱為過濾條件。在select語句中,資料根據where子句中指定的搜尋條件進行過濾,即where 子句用於過濾記錄,也就是where 子句用於提取那些滿足指定標準的記錄。where子句在表名 from子句 之後給出。where子句不僅用於select語法,...