简体   繁体   中英

Count not returning total count value in SQL

I have a MySQL database and it has a signUp table. The table has a field called created which is a placeholder for signups date. Format is eg: [2014-10-12 17:48:24].

There are many user who signup in single day. I am trying to get a count of each day signups.

My code below displays single date eg: [20141012000000] but the count is still 1.

SELECT DISTINCT TRUNCATE(created,-6) as created, 
       COUNT(*) as CountCreated 

FROM signUp 

WHERE created >= '20000101000000' AND created <= '30000101000000' 

GROUP BY created

---SQL RESULT---

|------------------------|-------------------------|
|Created                      CountCreated
|------------------------|-------------------------|
|20141012000000                  1
|------------------------|-------------------------|
|20141015000000                  1                
|------------------------|-------------------------|

There are more than 30 signup for that date. I used truncate so that the value is compared at the WHERE statement and its ommiting the TIME and only returning DATE . The time is zeroed.

What I am trying to get:

I simply want to get the total count of created field for that date. For example my signUp for [20141012000000] is 30 so I need it like this

|------------------------|-------------------------|
|Created                      CountCreated
|------------------------|-------------------------|
|20141012000000                  30
|------------------------|-------------------------|
|20141015000000                  33                
|------------------------|-------------------------|

Please help me fix this...

Thank You.

Assuming created is datetime :

SELECT date(created) as created,  COUNT(*) as CountCreated 
FROM signUp 
GROUP BY date(created);

If it is a string:

SELECT left(created, 10) as created,  COUNT(*) as CountCreated 
FROM signUp 
GROUP BY left(created, 10);

You'd also have to group by the truncated value in that scenario, and then get rid of the distinct. But a better option would simply be to make the created field a timestamp, and then use date(created).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM