1

下面是我在搜索中使用的查询

我在搜索中有日期范围,当我在“01/05/2012”和“31/05/2012”日期范围之间搜索数据但查询返回所有记录集时。

SELECT t.created date, t.saleid sale_id, u.id user_id, NULL merchant_name, cheque_number cheque_number, u.first_name name, (
SELECT company_name
FROM users
WHERE id = u.my_charity_id)charity_name, t.campaignname website, t.campaignid campaign_id, t.mysoko_discount discount_percentage, t.discount, t.salecommission sale_commission, t.salevalue total_sale_value, t.salestatus sale_status
FROM `transaction` AS `t` , `users` AS `u`
WHERE DATE_FORMAT( t.created, '%d/%m/%Y' )
BETWEEN '01/05/2012' AND '31/05/2012'
AND t.user_id = u.id
LIMIT 0 , 30

我正在寻找日期范围搜索数据,但结果返回表中的所有数据。

date            sale_id       user_id   
2012-04-19 00:00:00     20253305    45  
2012-04-11 00:00:00     20253306    68  
2012-04-23 00:00:00     20253307    68  
2012-04-25 00:00:00     20253308    45  
2012-04-27 00:00:00     20253309    45  
2012-04-29 00:00:00     20253310    68  
2012-04-30 00:00:00     20253311    45  
2012-05-01 00:00:00     20253312    45  
2012-05-03 00:00:00     20253313    68  
2012-04-18 00:00:00     20253314    4

我的搜索来自日期范围 txn 搜索(从/到日期)

输入日期格式:DD/MM/YYYY

4

4 回答 4

1

您可以尝试像这样过滤行:

WHERE t.created >= '2012-05-01'
  AND t.created <  '2012-06-01'

如果将月份指定为参数,则可以在 WHERE 子句中使用以下计算:

WHERE t.created >= @monthdate
  AND t.created <  @monthdate + INTERVAL 1 MONTH
于 2012-05-27T00:08:01.900 回答
1

做这个:

WHERE t.created BETWEEN '2012-05-01 00:00:00' AND '2012-05-31 23:59:59' ...

MySQL 会将 '2012-05-31 23:59:59' 解释为适当的 DATETIME 或 TIMESTAMP 类型,允许您利用t.created.

您的问题是您当前的查询指定字符串表示介于字符串“01/05/2012”和“31/05/2012”t.created之间。由于您将字符串格式设置为日-月-年,因此日组件介于“02”和“30”(含)之间的任何日期都将匹配。(并且五月或之后的任何一个月的第一个月都会匹配,等等)

于 2012-05-27T03:09:07.947 回答
0

一种选择:

 WHERE MONTH(t.created) = 5 AND YEAR(t.created) = 2012
于 2012-05-26T18:20:14.747 回答
0
SELECT t.created date, t.saleid sale_id, u.id user_id, NULL merchant_name, cheque_number cheque_number, u.first_name name, (
SELECT company_name
FROM users
WHERE id = u.my_charity_id)charity_name, t.campaignname website, t.campaignid campaign_id, t.mysoko_discount discount_percentage, t.discount, t.salecommission sale_commission, t.salevalue total_sale_value, t.salestatus sale_status
FROM `transaction` AS `t` , `users` AS `u`
WHERE DATE_FORMAT( t.created, '%d/%m/%Y' )
BETWEEN STR_TO_DATE('01/05/2012','%d,%m,%Y') AND STR_TO_DATE('31/05/2012','%d,%m,%Y')
AND t.user_id = u.id
LIMIT 0 , 30
于 2012-05-27T02:31:55.790 回答