2012-02-29 141 views
0

我使用下面的查询来获取我的表显示出两个日期

SELECT 
b.sales_title,c.cat_name,COUNT(b.sales_id) as cnt,COUNT(DISTINCT e.comment_id) as coun 
FROM tb_sale_report a 
INNER JOIN tbl_sales b on a.sales_id=b.sales_id 
INNER JOIN tb_category c on c.cat_id=b.category_id 
LEFT JOIN tb_comment e on b.sales_id=e.sales_id 
GROUP BY b.sales_title 

+------------+---------+--------+------+ 
| sales_title | cat_name| cnt | coun | 
+-------------+---------+--------+------+ 
| My Sales |Toys  |20  |5  | 
| First Sale |Dress |28  |1  | 
|Premium  |Computer |7  |16 | 
+-------------+--------+---------+------+ 

现在的结果之间筛选MySQL的结果tb_sale_report我也有一个名为view_date另一场将存储的日期在其中记录ID添加。现在我打算在tb_search_report表中添加一个筛选选项,用户可以在其中搜索在特定日期(例如开始日期和结束日期)之间添加的记录。我如何编写where条件来根据指定的日期过滤结果。 需要帮助。提前感谢。

回答

2

以下尝试:

SELECT 
b.sales_title,c.cat_name,COUNT(b.sales_id) as cnt,COUNT(DISTINCT e.comment_id) as coun 
FROM tb_sale_report a 
INNER JOIN tbl_sales b on a.sales_id=b.sales_id 
INNER JOIN tb_category c on c.cat_id=b.category_id 
LEFT JOIN tb_comment e on b.sales_id=e.sales_id 
where left(a.view_date,10) between 'start_date' and 'end_date'; 
GROUP BY b.sales_title 

Pleae替换起始日期,并用值

+0

非常感谢。它达到了目的。 – 2012-02-29 09:45:51

0

end_date之间的最简单的解决办法是只添加一个WHERE子句与BETWEEN sunclause,例如

WHERE view_date BETWEEN date1 AND date2 
+0

感谢您的帮助,但我希望有详细的解释。 – 2012-02-29 09:44:38

+0

你是说你想让我为你做所有的工作?很公平。 – 2012-02-29 09:46:31

+0

对不起,如果你觉得这样,但我不是故意的。我是一名初学者,在编程和自学过程中,因此我期望这一点。 – 2012-02-29 09:48:31

1

尝试和执行这个查询

SELECT 
b.sales_title,c.cat_name,COUNT(b.sales_id) as cnt,COUNT(DISTINCT e.comment_id) as coun 
FROM tb_sale_report a 
INNER JOIN tbl_sales b on a.sales_id=b.sales_id 
INNER JOIN tb_category c on c.cat_id=b.category_id 
LEFT JOIN tb_comment e on b.sales_id=e.sales_id 
WHERE b.sales_date BETWEEN $start_date AND $end_date 
GROUP BY b.sales_title