第4-6課データのフィルタリング

1669 ワード

実際の検索では、通常はすべての行を検索しません。データをフィルタリングし、必要条件に合うデータを選択します。
sqlのデータフィルタリングはwhere子文で指定された検索条件で行います。
where子文操作子
単一の値をチェック
select prod_name, prod_price
from products
where prod_price = 3.49;

select prod_name,prod_price
from products
where prod_price < 10;

select vend_id, prod_name
from products
where vend_id <> 'DLL01';

select vend_id, prod_name
from products
where vend_id != 'DLL01';
範囲値チェック
select prod_name,prod_price
from products
where prod_price between 5 and 10;
空の値をチェック
select prod_name
from products
where prod_name is null;
組み合わせwhere子文
and or操作符
select prod_name, prod_price
from products
where vend_id = 'DLL01' OR vend_id = 'BRS01'
and prod_price >= 10;

select prod_name, prod_price
from products
where (vend_id = 'DLL01' OR vend_id = 'BRS01')
and prod_price >= 10;
in操作子
select prod_name, prod_price
from products
where vend_id in( 'DLL01' , 'BRS01');
not操作子
select prod_name
from products
where not vend_id = 'DLL01'
order by prod_name;
ワイルドカードを使ってフィルタリングを行います。
likeオペレータを使って、通信検索を行います。
%は文字の任意の出現回数、fishの先頭の文字を表します。
select prod_id,prod_name
from products
where prod_name like 'Fish%';
%と似ていますが、1文字だけマッチします。
select prod_id,prod_name
from products
where prod_name like '__ inch teddy bear';
()ワイルドカードは文字セットにマッチするために使用されます。括弧内の任意の文字を指定する必要があります。
select cust_contact
from customers
where cust_contact like '[JM]%';