mysql在查询中选择优先级为3的字段

时间:2013-12-11 17:33:48

标签: mysql sql

我的表格和字段如下:

enter image description here

我必须找到$sy<year<$ey然后它必须最后按$sm<month<$em过滤值,必须找到$sd<day<$ed

我需要在日期之间找到记录,例如2010/10 / 25,2010 / 10/10

起初我试过了:

SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result 
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;

但它无法找到像2010/10/25,2010 / 10/28这样的日期的记录

比我试过

SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
在我的情况下,这不是好事!

我需要一些像优先选择首先选择年份而不是月份而不是一天!!

其他方式是将mysql记录按年份和月份和日期转换为时间戳,并按输入日期进行比较,但UNIX_TIMESTAMP('year-month-day 00:00:00')对我来说不正确。 我用过它:

SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453

1 个答案:

答案 0 :(得分:1)

如果转换为时间戳不适合您,请使用date_format进行转换:

SELECT * 
  FROM `work_result` 
 WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') > 
         DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')