2014-04-18 197 views
0

我有一个表格,例如以2014-12-05的形式存储两个日期。这些日期是从日期到日期。我想从不同的表中选择项目,其中也包含日期列。所以我想要做类似如下的事情:在日期之间选择语句sqlite

SELECT * FROM TABLE2 WHERE date BETWEEN fromdate AND todate 

除了fromdate和todate列来自table1,而'date'来自table2。有没有一个简洁的方法来做到这一点?

回答

0

您会在两个表格之间进行某种连接,并从连接中选择条目。逗号分隔您正在查询的表格可以实现简单的笛卡尔连接。

create table holidays (
     name text not null, 
     fromdate text not null, 
     todate text not null 
); 

create table appointments (
     name text not null, 
     date text not null 
); 

insert into holidays values ('Christmas Holiday', '2014-12-05', '2014-12-24'); 

insert into appointments values ('Dentist', '2014-11-06'); 
insert into appointments values ('Doctor', '2014-12-06'); 

select h.name, a.name, a.date 
     from appointments a, holidays h 
     where a.date between h.fromdate and h.todate; 
+0

辉煌的,在我见过的所有代码解释中,这真的是最清晰的一个。它的工作非常感谢! – Mohd