比较变量然后设置一个变量(IF sql)

时间:2017-09-15 08:54:19

标签: mysql sql

尝试并且未能在SQL SELECT(MySQL)中发现IF或IIF的正确语法

比较2个变量,如果不同,则将另一个变量设置为值

IF(assetID <> previousAssetID) set @ts = vardtmin

如果语句解析为true,我是否需要另一个sts为1的说明符?

完整的SELECT是 -

SELECT 
assetID, 
IF(assetdID <> previousAssetID) set @ts=vardtmin, 
nodeid, 
@changed := IF(nodeid <> previousLocationID, @changed + 1, @changed) AS changed, 
IFNULL(TIMESTAMPDIFF(SECOND, previousTs, ts), 0) AS secDiff

此致 活性

1 个答案:

答案 0 :(得分:0)

我不知道MYSQL,但SQL语法如下: -

SELECT 
assetID, 
@ts = case when assetdID <> previousAssetID then vardtmin else 111 end , 
nodeid, 
@changed = case when  nodeid <> previousLocationID @changed + 1 else  @changed AS changed, 
case when (TIMESTAMPDIFF(SECOND, previousTs, ts), 0) isnull then .. else .. end  AS secDiff