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

Mysqlにて、datetime型のデータに対してdate型でgroupbyする

Last updated at Posted at 2019-10-10
select date_format(cu.created_at, '%Y/%m/%d') as 日付,
       count(cu.user_id)                      as 利用者数
from coupon_used as cu
where cu.code in ("2019", "2018")
  and cu.created_at >= "2018-01-01 00:00:00"
  and cu.created_at < "2020-01-01 00:00:00"
group by date_format(cu.created_at, '%Y/%m/%d');

date_format便利

ちなみに、asで定義したdate_formatはgroup byで使用できた。

select date_format(cu.created_at, '%Y/%m/%d') as 日付,
       count(cu.user_id)                      as 利用者数
from coupon_used as cu
where cu.code in ("2019", "2018")
  and cu.created_at >= "2018-01-01 00:00:00"
  and cu.created_at < "2020-01-01 00:00:00"
group by 日付

group by 日付面白い

変数定義みたいにindex保存されてるのかな

SqlでLoopとかも書いてみたいけど、その域になったらクエリビルダ使用したほうがいいな笑

0
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
0
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?