我在搜索中使用的查询
我在搜索中有日期范围,当我在'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
答案 0 :(得分: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
答案 1 :(得分: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
上的索引。
您的问题是您当前的查询指定t.created
的字符串表示位于字符串 '01 / 05/2012'和'31 /之间05/2012' 。由于您将字符串格式设置为日 - 月 - 年,因此日期组件介于“02”和“30”之间的任何日期都将匹配。 (并且五月或之后任何一个月的第一天将匹配,等等。)
答案 2 :(得分:0)
一个选项:
WHERE MONTH(t.created) = 5 AND YEAR(t.created) = 2012
答案 3 :(得分: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