简体   繁体   English

如何在MySQL中显示某一天的订单总数

[英]How to Display Total number of Orders on a Certain Day in MySQL

I would like to know how to display orders placed on a certain day. 我想知道如何显示某一天的订单。

For example: 例如:

I would like to display orders placed today. 我想显示今天下的订单。

My MySQL database contains the following tables: 我的MySQL数据库包含下表:

1) orders 1)订单
2) orders_statuses 2)orders_statuses

Under orders , I have the following fields: order下 ,我有以下字段:
1) orders_id 1)orders_id
2) orders_placed_date 2)orders_placed_date

Under orders_statuses , I have the following fields: orders_statuses下 ,我具有以下字段:
1) status_id 1)status_id
2) status_name 2)status_name

Whenever a new order is placed, it gets the default status_id = 3 (meaning status_name= Order is Pending) 每当下新订单时,它将获得默认的status_id = 3(表示status_name =订单待处理)

So, if today 3 orders were placed, all of those will have the same status_id, which is 3. (Order is Pending) 因此,如果今天下了3个订单,那么所有这些都将具有相同的status_id,即3。(订单待处理)

Now, what query should I use to calculate orders placed today, and this week and this month? 现在,我应该使用什么查询来计算今天以及本周和本月下的订单?

Here is the query where I select all the orders placed in my cart: 这是我选择购物车中所有订单的查询:

SELECT   order_id, order_placed_date FROM  orders

The Result of the above query: 以上查询的结果:

Order ID ---- Order Date Placed 订单ID ----下订单日期
1 --- 12/30/2008 12:06:24 AM 1 --- 12/30/2008 12:06:24 AM
2 --- 2/3/2009 1:57:17 AM 2 --- 2/3/2009 1:57:17 AM
3 --- 2/3/2009 1:58:27 AM 3 --- 2/3/2009 1:58:27 AM
4 --- 5/3/2009 1:58:48 AM 2009年4月4日-5/3 1:58:48
5 --- 6/3/2009 2:00:31 AM 5 --- 6/3/2009 2:00:31 AM
6 --- 7/3/2009 2:01:47 AM 6 --- 7/3/2009 2:01:47 AM
7 --- 7/3/2009 2:02:31 AM 2009年7月7日-7/3/2 2:02:31
9 --- 7/4/2009 2:21:18 PM 9 --- 7/4/2009 2:21:18 PM
10 --- 7/4/2009 2:21:36 PM 2009年10月7日-7/4/21下午21:36
11 --- 7/4/2009 2:22:18 PM 2009年11月7日-7/4/22下午22:18
12 --- 7/4/2009 2:23:29 PM 12 --- 7/4/2009 2:23:29 PM
13 --- 7/4/2009 2:24:24 PM 13 --- 7/4/2009 2:24:24 PM

What I would like is to get the total number of orders placed today. 我想得到的是今天下的订单总数。

Since today's date is 4 July 2009, you can see from the above result that there are 5 orders placed on 4th July. 由于今天是2009年7月4日,因此从上述结果可以看出,7月4日有5个订单。

What query should I use to get the total number of orders in a certain day, in today's case, 5. 在今天的情况下,我应该使用哪种查询来获取某一天的订单总数(5)。

UPDATE : Solved 更新:已解决

I used both Adam's and colithium querys to get the answer: 我同时使用了Adam和colithium查询来获得答案:

Here is what I used: 这是我使用的:


SELECT   COUNT(order_placed_date)
FROM     va_orders
WHERE    DATE(order_placed_date) = curdate();

It worked great :) 它很棒:)

Assuming you have a foreign key in ORDERS_STATUSES called ORDERS_ID : 假设您在ORDERS_STATUSES有一个名为ORDERS_ID的外键:

select o.orders_id
       , o.orders_placed_date
from   orders o
       inner join orders_statuses os
       os.orders_id = o.orders_id
    and
       os.status_id = 3
where  date(o.orders_placed_date) = curdate()

To get the number of orders on a certain day: 要获取某一天的订单数量:

SELECT COUNT(*) FROM orders WHERE order_placed_date = @DayYouWant

To get the orders from today/this week/this month: 要获取今天/本周/本月的订单:

SELECT order_id, order_placed_date FROM orders WHERE order_placed_date BETWEEN @StartDate AND @EndDate

If you want only orders with a status of 3 you will need to JOIN with the status table and add that criteria to the WHERE clause. 如果只需要状态为3的订单,则需要与状态表联接,并将该条件添加到WHERE子句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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