1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLの基礎 select,where,limit

Last updated at Posted at 2021-03-24

こちらの記事では、私が学んだSQLについて記載しております。
自身の忘れないためのメモとしております。
間違っている部分もあるかもしれませんので、ご了承ください。

selectを使用して、テーブルのデータを取得する

・テーブルから一部のカラムを取る場合
select 列1(カラム),列2(カラム名)・・・ from テーブル名;

・全ての列を取得したい場合
select * from テーブル名;

テーブルのカラムを取得し、カラム名を変更する

・テーブルからnameというカラム名を取る場合
select name from テーブル名;

・取得したnameを、「名前」と日本語に変更する場合
select name as 名前 from テーブル名;
select name 名前 from テーブル名; ←asを省略してもOK

取得したデータから計算をして取得することも可能

・価格のカラム(price)から、税込価格を入れて取得したい場合
select price * 1.1 税込価格 from テーブル名;
↑ 
追加でpriceを、税込価格と変更して取得しています。

・商品名も入れて行う場合
select name 名前, price * 1.1 税込価格 from テーブル名;
↑
商品名と税込価格の両方を取得できる!

条件をつけたい時に、whereを使用する

・商品の金額が5000円以上の商品のみを取得する場合 
select name, price from テーブル名 where price >= 5000;

・name(商品名)とprice(価格)5000円以上の取得が可能になる!

演算子の種類

記号 意味      記号 意味                 
= 等しい in ある値がセット内に含まれているかどうか
> より大きい not in 値がセット内に含まれていないかどうか
< より小さい is null 値がnull   
>= 以上   is not null 値がnullでない  
<= 以下   like あいまい検索   
<>, != 等しくない betwee..and.. 値が値の範囲内に含まれているかどうか

演算子とwhereを使用した書き方

・テーブルのid 1を取得する場合
select * from テーブル名 where id = 1;

・田中さんのみを取得する場合
select * from テーブル名 where name = '田中';

・1000以上を取得する場合
select * from テーブル名 where price > 1000;

・1000以下を取得する場合
select * from テーブル名 where price < 1000;

・1000以外を取得する場合
select * from テーブル名 where price <> 1000; *!=も同じ

・idの1、2、3のみを取得する場合
select * from テーブル名 where id in(1,2,3);

・idの1、2、3以外を取得する場合
select * from テーブル名 where id not in(1,2,3);

・null以外を取得する場合
select * from テーブル名 where price is not null;

・nullのみ取得する場合
select * from テーブル名 where price is null;

・1000から2000までの範囲を取得する場合 (書き方2種類)
select * from テーブル名 where price between 1000 and 2000;
select * from テーブル名 where price >= 1000 and price <= 2000;

・田中もしくは鈴木を取得する場合
select * from テーブル名 where name = '田中' or name = '鈴木';

・likeでの検索を行う場合 ''中に、%もしくは_を使用する
select * from テーブル名 where name like '田%';  '田'で始まる文字列を取得
select * from テーブル名 where name like '%田%'; '田'を含む文字列を取得
select * from テーブル名 where name like '%郎';  '郎'で終わる文字列を取得
select * from テーブル名 where name like '_郎';  何かしら2文字から始まり、'郎'で終わる文字列

limitを使用して、取得の制限をかける

・limitの記述方法 オフセットはなくても良い!
select カラム名 from テーブル名 limit [オフセット,] 最大取得数;

・10件を取得する場合
select * from テーブル名 limit 10;  

・1から10件を取得する場合 どこから取得したいかを制限する  
下記は1件目から10件目を取得される
select * from テーブル名 limit 0, 10; 注意 1件目から取得する場合は、0から開始すること

・10件目から10件取得する場合 下記の場合だと11件目から20件目までを取得される。
select * from テーブル名 limit 10, 10;

補足

エラーが起こった場合は、下記三点を確認する!!

-DBの選択をしていない。
-スペルミスをしていないか。
-全角文字になっていないか。
上記3点で解決しなければ、エラーコードから検索して行うと早く発見できる可能性がある。

過去に行ったSQLコードを再度読み込みたい場合

Action Outputから右クリックを押し、「Append Selected items to SQL script」をクリックすると過去のコードを読み起こすことが可能です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?