选择要在视图中保留哪些冲突条目

时间:2013-05-31 05:07:57

标签: sqlite

假设我有一个像这样的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查询。

有没有办法可以根据某些条件(比如最近的条件)选择要保留在视图中的哪个冲突条目?

1 个答案:

答案 0 :(得分: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)