My query return like below
date count
November 19,2019,11:58 PM 2
November 19,2019,11:59 PM 2
November 20,2019,12:02 AM 2
Is there any way to do like
November 19,2019 4
November 20,2019 2
follow this query:
I get this result when I execute the query
select count(*) from listings group by date(created)
Seems your date is not a standard mysql date time format. We need to format it first using str_to_date function.
select cast(str_to_date(dateC, '%M %d,%Y,%h:%i') as date), count(1) from Test
group by cast(str_to_date(dateC, '%M %d,%Y,%h:%i') as date)
see dbfiddle.
Related
We have mysql table with below columns. I am given startDate and endDate and now I have to fetch rows between those dates. Can some one please help with mysql query with how I can get those rows?
For example if startDate is November 2021 and endDate is March 2022. Then query should select rows with the months between these dates.
month
year
values
09
2021
30
10
2021
40
11
2021
90
12
2021
10
01
2022
25
02
2022
15
03
2022
89
You can compare tuples in MySQL:
select month, year, `values`
from tbl
where (year, month) >= ('2021', '12')
and (year, month) <= ('2022', '03');
Another way is combining year and month to a single string:
select month, year, `values`
from tbl
where concat(year, month) between ('202112') and ('202203');
This only works if year and month (at least month) are zero-filled strings.
I think the first step would be to compute a column with a proper DATE value, starting from the column you have.
For example, STR_TO_DATE(CONCAT(year,'-', month, '-', 1), '%Y-%m-%d') will return a proper SQL date on the 1st day of the year-month that you consider.
This value can then be compared (with <= and >=) with the limit dates (as requested in the comments, the answer depends on how those are represented).
I'm working to write a MySQL query that outputs the number of new users created by week.
My user table is:
id | created_at
My query:
SELECT YEAR(created_at) AS Year, DATE_FORMAT(created_at, '%b %e') AS Week, COUNT(*) AS total
FROM users
GROUP BY Year, Week;
The problem:
Years: I would like the most recent year to be at the top, currently the oldest year is at the top of the output.
Weeks: The week column is not sorted based on the calendar. For example, the last records shows: 2019 | May 9 | 100
Where I'd like the year and week sorted.
I think you'll find the function YEARWEEK() helpful, not only for the grouping, but also for the ordering.
Also, your use of DATE_FORMAT() doesn't look right, because you're outputing the %e, which is the day of the month, yet you're grouping by week?
SELECT DATE_FORMAT(created_at, '%Y %b %v') AS date, COUNT(*) AS total
FROM users
GROUP BY YEARWEEK(created_at)
ORDER BY YEARWEEK(created_at) DESC;
I have a list of users and their birthdays. I need to print the list of birthdays for each month and grouped by day. For example:
Monday 24th (2): Herpina Derpina, John Doe
Tuesday 27th (1): Nicolas Cage
Friday 28th (1): Michael Jackson
How could I achieve that with one single SQL call? If I group them this way, I will lose records:
SELECT COUNT(*) FROM user GROUP BY MONTH(birthday)
Any ideas? (I'm using mysql + php)
You can't get the count which is an aggregate function and the detailed records in the same query. If the names are essential then you will have to manually construct the counts in PHP and then display the records under the required format.
SELECT MONTH(birthday) month,
FORMAT(birthday, '%W %D') day,
COUNT(user) count,
GROUP_CONCAT(user SEPARATOR ',') users
FROM table
GROUP BY MONTH(birthday), FORMAT(birthday, '%W %D')
I have a date field in my table called date1. if i use the following query.
select * from schedule order by date1 asc
it gives the result like as jan 2011 comes before december 2010. but i need the december 2011 as the first row of the result.
Change your query to
SELECT * FROM schedule ORDER BY date1 DESC
This should do the trick.
James
select * from schedule order by date1 desc
I want to get the last 10 unique months where there have been events, stored as event_date as a mysql date. I am using coldfusion so could group the result but can't get that working as I would like.
This is what I have so far.
SELECT MONTH(event_date) AS month, YEAR(event_date) AS year, event_date from events
so ideally if there were no posts in August would be output as
september 2010
july 2010
june 2010
may
etc etc
Thanks.
R.
SELECT DISTINCT MONTH(event_date) AS month, YEAR(event_date) AS year from events ORDER BY year DESC, month DESC LIMIT 10;