MySQL-按日期分组和计数

发布于 2024-10-25 07:41:16 字数 415 浏览 4 评论 0原文

我正在尝试找出获取我需要的查询结果的正确语法。场景如下:

我正在设计一个 SaaS 应用程序,有一个名为 trips 的表,如下所示:

table.trips  
- trip_id (pk, ai)  
- client_id (fk)  
- shop_id (fk)
- trip_date (date)

我需要能够获取表中每天的行程数(按 shop_id),然后提供每个行程的 TRIPS 数trip_date,例如:

DATE         | TRIP SUM  
--------------------------
01-01-2011   | 3
01-02-2011   | 2  
01-03-2011   | 5

对正确语法有什么想法吗?

I am trying to figure out the proper syntax for getting query result I need. Here is the scenario:

I am designing a SaaS application and have a table called trips, like so:

table.trips  
- trip_id (pk, ai)  
- client_id (fk)  
- shop_id (fk)
- trip_date (date)

I need to be able to get the number of trips in the table for each day (by shop_id) and then deliver the number of TRIPS per trip_date, like:

DATE         | TRIP SUM  
--------------------------
01-01-2011   | 3
01-02-2011   | 2  
01-03-2011   | 5

Any thoughts on the proper syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月亮是我掰弯的 2024-11-01 07:41:17

如果日期列还存储小时和分钟,则需要对日期应用子字符串函数,以仅包含日期列的 yyyy-mm-dd 部分,如下所示:

SELECT COUNT(*), substring(trip_date,1,10)
FROM trips
GROUP BY substring(trip_date,1,10);

If the date column also stores hour and minute you will need to apply a substring function to the date, to only have the yyyy-mm-dd part of the date column, like this:

SELECT COUNT(*), substring(trip_date,1,10)
FROM trips
GROUP BY substring(trip_date,1,10);
我不会写诗 2024-11-01 07:41:17

像这样的东西吗?

select 
   DATE(trip_date) as [DATE],
   count(1) as [TRIP SUM]
from
   trips
group by
   DATE(trip_date), shop_id

我不是 mySQL 人员,但您需要使用 T-SQL DatePart 等效项,其中我使用了 DATE() 函数来删除日期的时间部分。

Something like this?

select 
   DATE(trip_date) as [DATE],
   count(1) as [TRIP SUM]
from
   trips
group by
   DATE(trip_date), shop_id

I'm not a mySQL guy, but you will need to use a T-SQL DatePart equivalent, where I have used the DATE() function in order to strip the time portion of the date.

超可爱的懒熊 2024-11-01 07:41:17
select trip_date,count(*)
from trips
where shop_id=[the shop id]
group by trip_date

只要日期采用您显示的格式,此操作就有效。如果它也有时间那么你必须删除时间。

另外我不知道 client_id 是否必须出现在这个查询中,如果你也必须按它分组,那么你必须将它放在 select 或 group 上的 trip_date 旁边。如果你想通过它进行过滤,请将其放在 where 中。

select trip_date,count(*)
from trips
where shop_id=[the shop id]
group by trip_date

This will work as long as the date is on the format you showed. If it has also time then you have to remove the time.

Also I do not know if the client_id has to come in in this query, if you have to group by it also then you have to put it along side trip_date on select or group. If you want to filter by it put it in the where.

少女七分熟 2024-11-01 07:41:17
SELECT shop_id, trip_date, count(trip_id)
FROM trips
GROUP BY shop_id, trip_date
SELECT shop_id, trip_date, count(trip_id)
FROM trips
GROUP BY shop_id, trip_date
蘸点软妹酱 2024-11-01 07:41:16
SELECT COUNT(*), trip_date , shop_id 
FROM trips 
GROUP BY trip_date , shop_id
SELECT COUNT(*), trip_date , shop_id 
FROM trips 
GROUP BY trip_date , shop_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文