2
1

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 5 years have passed since last update.

【SQL】SELECT文あれこれ

Last updated at Posted at 2019-09-08

SQLの条件検索のあれこれです。
※今回もメモ
※修正あれば都度修正します。
※2019/9/14修正

①名前に「hoge」とつく人だけを取得する(取得カラム:全て)

SELECT * FROM `テーブル名` WHERE name LIKE '%2号%'

②誕生日が「2019-01-01 00:00:00より後」の人だけを取得する(取得カラム:全て)

SELECT * FROM `テーブル名` WHERE birthday > '2019-01-01 00:00:00'

③誕生日が「現在時刻から100日前より後」の人だけを取得する(取得カラム:全て)

SELECT * FROM `テーブル名` WHERE birthday > ( NOW( ) - INTERVAL 100 DAY )

④それぞれの人の誕生月を取得する。誕生月の名称は「as」を使って「birth_month」する
(取得カラム:name、birth_month)

SELECT name, MONTH(birthday) as birth_month FROM `テーブル名`

⑤④の派生。それぞれの誕生月に該当する人が何人いるかを「group by」を使って取得する(取得カラム:birth_month、何人いるか(カラム名は任意))

SELECT MONTH(birthday) as birth_month, COUNT(birthday) FROM `テーブル名` GROUP BY birth_month

今回も勉強なったわー。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?