我已经构建了这个SQL查询,该查询应该将最高日期值放在field_result
内,但它并没有按预期工作。
SELECT
field_one,
field_two,
IF(((field_one IS NOT NULL AND field_one <> '') AND
(field_one > TableB.field_two)),
unix_timestamp(field_one),
unix_timestamp(TableB.field_two)) AS result
FROM TableA
LEFT JOIN TableB ON TableB.id = TableA.id
条件非常简单。
field_one
必须存在,并包含某种价值。field_one > field_two
显示field_one
,则显示field_two
它产生结果
"field_one", "field_two", "field_result"
"1495681346","1500328106","1500328106"
"1500342725","1500328106","1500328106"
"1499922505","1500329807","1500329807"
"1500343148","1500328106","1500328106"
"1500329593","1500328106","1500328106"
由于某些原因row 2
,field_result
的值不是1500342725
(此值大约14619
大于1500328106
。
我认为我的语法正确,但我不确定,因为结果不是我所期待的
答案 0 :(得分:1)
如果要查找两个字段中的较大字段,请考虑使用MySQL的GREATEST()
函数。
SELECT
field_one,
field_two,
GREATEST(COALESCE(UNIX_TIMESTAMP(field_one), -1),
COALESCE(UNIX_TIMESTAMP(field_two), -1)) AS result
FROM TableA