根据操作添加列以查询结果

时间:2014-07-31 11:21:18

标签: php mysql sql

我们正在尝试实现增量同步。

我们有一个OBJECTS这样的表:

ID | ACTIVE | CREATED | MODIFIED | CONTENT
1    1        1234      1235       text

客户端向服务器发送他的上一个同步日期(一个长整数)。

[S = LastSync; C = created; M = Modified]

查询仅选择M>S的行。

当需要新列ACTION时可以0(插入),1(更新)或2(删除)[用于告诉客户什么做]。

ACTION将遵循以下规则:

if(C==M && S<M) return 0; //Object created, not modified, last sync before creation

if(C<M && C<S<M && ACTIVE == 1) return 1; //Object modified, still active, sync between creation and modification

if(C<M && C<S<M && ACTIVE == 0) return 2; //Object modified, not active, sync between creation and modification

if(C<M && S<C) return 0; //Object modified, last sync before creation

有没有办法通过MySQL中的查询来完成所有这些操作?

1 个答案:

答案 0 :(得分:0)

我为你的&#39; 4-if条件创建了一个sql查询逻辑&#39; (仅)...尝试它是否适合您:

select ((created<modified) + (created<LastSync) + (LastSync<modified) - ((active or LastSync<created) and (created<Modified)) - 1) as action;

相关问题