我有一个带有待办事项数据库的待办事项应用程序。它包含一个done列,该列将设置为 false 或 true。现在我想按all或unfinished过滤数据。所以要么 done 是无关紧要的,要么它必须是false。
我在 TypeScript 应用程序中使用 SQLite3。目前我通过使用字符串模板来做到这一点,但我更喜欢基于 SQL 的解决方案。
db.prepare(`
select
rowid as id,
title,
description
from todo
${selectAll ? '' : 'where done = 0'}
limit ?
offset ?
`).all(limit, offset);
我的想法是使用该CASE
子句,但似乎无法解决该WHERE
子句。
有没有更好的解决方案?
selectAll
是根据应用程序的查询参数设置的 TypeScript 变量。
selectAll
为假时输出
id | title | description | done
1 | Clean the kitchen | as the title says... | 1
2 | Do the shopping | potatoes, tomatoes | 0
3 | Program stuff | Todo app | 1
selectAll
为真时输出
id | title | description | done
2 | Do the shopping | potatoes, tomatoes | 0