对于我的项目,我需要从此表中选择行
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| hardware_type_id | int(11) | NO | MUL | NULL | |
| make_type_id | int(11) | NO | MUL | NULL | |
| year | int(11) | YES | | NULL | |
| model | varchar(255) | YES | | NULL | |
| name | varchar(255) | NO | | NULL | |
| warranty | date | YES | | NULL | |
+------------------+--------------+------+-----+---------+----------------+
保修在搜索时仍然有效。 (如果保修为空,则不要选择该行)
基本上我需要这样的东西:
SELECT * FROM hardware WHERE ({now} - warranty) <0; // valid warranty
SELECT * FROM hardware WHERE ({now} - warranty) >0; // invalid warranty
答案 0 :(得分:1)
像Mihai已经说过的那样
SELECT * FROM hardware WHERE warranty <= CURDATE(); // valid warranty
SELECT * FROM hardware WHERE warranty > CURDATE() OR warranty IS NULL; // invalid warranty
这应该有用,如果是日期时间或时间类型,你也可以使用NOW()或CURTIME()
答案 1 :(得分:0)
您可以像这样使用DATEDIFF
:
SELECT *,
IF(DATEDIFF(NOW(), warranty) > 0, false, true) AS IsWarrantyValid
FROM hardware
如果DATEDIFF(NOW(), warranty) > 0
这意味着warranty
在当前日期之前,那么它已过期。因此,IF
表达式在这种情况下返回false。
答案 2 :(得分:0)
if(warranty > (now()-warranty))
then
select * from hardware where warranty > (now()-warranty) ;
end if ;