MySQL-按日期分组和计数
我正在尝试找出获取我需要的查询结果的正确语法。场景如下:
我正在设计一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果日期列还存储小时和分钟,则需要对日期应用子字符串函数,以仅包含日期列的 yyyy-mm-dd 部分,如下所示:
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:
像这样的东西吗?
我不是 mySQL 人员,但您需要使用 T-SQL DatePart 等效项,其中我使用了 DATE() 函数来删除日期的时间部分。
Something like this?
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.只要日期采用您显示的格式,此操作就有效。如果它也有时间那么你必须删除时间。
另外我不知道 client_id 是否必须出现在这个查询中,如果你也必须按它分组,那么你必须将它放在 select 或 group 上的 trip_date 旁边。如果你想通过它进行过滤,请将其放在 where 中。
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.