我有一个类似的查询..它不起作用,但是,我在WHERE NOT EXISTS位上遇到语法错误。但是我不确定它会按照我想要它的方式工作。我想做的就是让它在任何一天只能有一个'B',它将是第一个实例..但我不确定下面的代码是否有效,无论如何,任何事情都会是'B'。
UPDATE FoodIntake
SET MealTypeCode = CASE
WHEN substr(DateTime, -8)
BETWEEN (SELECT BreakfastStart FROM Patient WHERE PatientId = :PatientId)
AND (SELECT LunchStart FROM Patient WHERE PatientId = :PatientId)
AND CarbAmount >= 25
AND MealTypeCode IS NULL
WHERE NOT EXISTS (
SELECT * FROM FoodIntake as Old
WHERE MealTypeCode = 'B'
AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10)
)
THEN 'B'
END
我不确定如何处理的另一个选项是标记应用为B的所有内容,然后执行第二个查询以查找一天中有2个B并且将第二个重命名为其他内容的任何时间
我有一个我试过的查询,但我知道它有一些重大问题..有一些语法错误我无法弄清楚,但我认为它至少可以解决我想做的事。
UPDATE FoodIntake
SET MealTypeCode = 'U'
WHERE MealTypeCode = 'B'
AND count(SELECT 1 FROM FoodIntake as Old WHERE MealTypeCode ='B' AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10) ) > 1
AND FoodIntake.DateTime > (SELECT DateTime FROM FoodIntake as Old WHERE MealTypeCode ='B' AND substr(Old.DateTime, 10) = substr(FoodIntake.DateTime, 10) ORDER By DateTime ASC LIMIT = count-1)
答案 0 :(得分:0)
我认为你最好使用IF ... ELSE
语句,虽然可能更详细一点,但更明确地说明了这里发生的逻辑。
IF (SELECT COUNT(1) FROM FoodIntake
WHERE (date('now') BETWEEN date('now','start of day')
AND date('now','start of day','+1 day') )
AND MealTypeCode = 'B') > 0
-- Make update based on 'B' code already taken
ELSE
-- Make update based on 'B' code not yet taken
我不是一个SQLite人,但我对mySQL和T-SQL很好,所以如果日期函数不准确,我引用了这篇文章:http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions