0

假设我有一个这样的 Sqlite 表:

Name       |Date of Appointment |Time of Appointment
------------------------------------------------
Jane Doe   |May 1st, 2013       |09:00
Jane Doe   |May 1st, 2013       |15:00
John Doe   |May 2nd, 2013       |08:00
John Doe   |May 3rd, 2013       |12:00

在示例中,Jane Doe 在同一天有两个约会,但 John Doe 在不同的日子有两个不同的约会。

现在假设我想创建一个表格视图来显示特定日期的所有约会。我可以使用以下语法为 5 月 1 日创建一个视图:

CREATE VIEW IF NOT EXISTS [May 1st Appointments] AS SELECT * FROM AppointmentsTable WHERE [Date of Appointment] = 'May 1st, 2013';

这将创建一个如下所示的视图:

Name       |Date of Appointment |Time of Appointment
------------------------------------------------
Jane Doe   |May 1st, 2013       |09:00
Jane Doe   |May 1st, 2013       |15:00

但是,查看 Jane Doe 的多个约会可能看起来令人困惑,所以我只想在视图中显示最近的约会。我看到我可以使用ON CONFLICT子句,但似乎我总是必须保留第一个条目并忽略第二个条目,用第二个条目替换第一个条目,或者停止 SQLite 查询。

有没有一种方法可以让我根据某些条件(例如最近的)选择要保留在视图中的冲突条目?

4

1 回答 1

1

如果我是正确的,如果在特定日期存在多个约会,您想要的是特定日期的最新约会

然后请尝试下面的查询它应该可以工作

CREATE VIEW IF NOT EXISTS [May 1st Appointments] AS SELECT * FROM AppointmentsTable a where [Date of Appointment] = 'May 1st, 2013' and [Time of Appointment]=(select [Time of Appointment] from AppointmentsTable where Name= a.Name and [Date of Appointment] = 'May 1st, 2013' order by [Time of Appointment] desc limit 1)
于 2013-05-31T07:13:57.790 回答