SQLite CASE,如果从SUB QUERY结果

时间:2013-04-07 10:02:48

标签: sql sqlite subquery case

我在SQLite 3数据库中有以下SQL查询

  UPDATE Events 
  SET location = CASE 
        WHEN identifier = (SELECT id FROM users_db.exerciseLists WHERE patientId = ? AND complete = 1) 
        THEN 'yellow' 
        WHEN identifier = ? 
        THEN 'green' 
        WHEN date_start < datetime('now') 
        THEN 'red' 
        WHEN date_start > datetime('now') 
        THEN 'blue' 
        ELSE 'green' 
        END, 
  type = CASE 
        WHEN identifier = ? 
        THEN 'yes' 
        ELSE 'no' 
        END

子查询

  (SELECT id FROM users_db.exerciseLists WHERE patientId = ? AND complete = 1) 

返回多个结果,但上述案例查询仅使用第一个返回的结果。

我做错了什么?

由于

1 个答案:

答案 0 :(得分:2)

如果子查询返回多个结果并且您想要更新所有返回结果,那么最好使用这样的IN语句:

WHEN identifier IN (SELECT id FROM users_db.exerciseLists WHERE patientId = ? AND complete = 1)