以下是我的查询,其中我试图在users.i_dob上应用公式,如果插入值不为零,则查询应插入零。请让我知道如何修改以下实际查询以实现我的功能,谢谢,
实际查询:
INSERT IGNORE into age ( i_age)
SELECT DISTINCT FLOOR((TO_DAYS(NOW())- TO_DAYS(FROM_UNIXTIME(users.i_dob))) / 365.25)
FROM users
我要对查询做些什么:
INSERT IGNORE into z_census ( i_age)
SELECT IF (i_age == 0) i_age else FLOOR((TO_DAYS(NOW())- TO_DAYS(FROM_UNIXTIME(users.i_dob))) / 365.25)
FROM users
答案 0 :(得分:1)
请参阅MySQL IF语法。看起来应该是这样的:
INSERT IGNORE into z_census (i_age)
SELECT IF(i_age=0, i_age,
FLOOR((TO_DAYS(NOW())- TO_DAYS(FROM_UNIXTIME(users.i_dob))) / 365.25))
FROM users
答案 1 :(得分:1)
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
INSERT IGNORE into z_census ( i_age)
SELECT IF((i_age = 0), i_age, FLOOR((TO_DAYS(NOW())- TO_DAYS(FROM_UNIXTIME(users.i_dob))) / 365.25))
FROM users