---
title: "mysql sql 用户一天领取数计算"
author: "anlinko"
date: "2020-03-02T17:13:20Z"
tags: ["SQL"]
categories: ["数据库"]
slug: "mysql-sql"
draft: false
lastmod: "2020-03-02T17:13:20Z"
id: 2512743
url: "https://anlinko.iteye.com/blog/2512743"
---

```sql


 select
        t.id
        ,t.title
        ,t.subtitle
        ,t.num   -- 该券生成总数量
        ,t.limit_draw_num   -- 每人限领数量
        ,t.limit_day_draw_num -- 每天每天限领数量
        ,t.created_at
        ,t.updated_at
        ,t.tag
        ,t.duration
        ,count(1) drawCount  -- 该券被所有用户已经使用的总数量
        ,count(case when ut.user_id=#{userId} then 1 end) as userDrawCount  --该用户已经使用该券的总数量
        ,count(
        case when ut.user_id=#{userId} and ut.created_at BETWEEN UNIX_TIMESTAMP(CAST(SYSDATE()AS DATE)) and UNIX_TIMESTAMP(CAST(SYSDATE()AS DATE) - INTERVAL -1 DAY) then 1 end
        )  as userDrawCountToday -- 该用户使用今天使用该券的使用
        from qyw_ticket t
        left join qyw_user_ticket ut
        on ut.ticket_id=t.id
        where 1=1   and and t.id in (1)
-------------------------------------------------------------------------------------------------------------
389	3300013	19700119024324105547567652	9		0	1579404982	1579404982	false		1578810396		
390	3300013	19700119024324061152323077	10		0	1579404982	1579404982	false		1578810396		
391	3300013	19700119024324077674041051	11		0	1579404983	1579404983	false		1578810396		
392	3300013	19700119024325385064852205	9		1	1579405027	1579405027	false		1605325027		
393	3300013	19700119024325157232247884	10		1	1579405027	1579405027	false		1605325027		
394	3300013	19700119024325704365887058	11		1	1579405027	1579405027	false		1605325027		





```