2012-02-29 50 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