Question

I have one table storing two dates in the form: 2014-12-05 for example. Those dates are date from and date to. I want to select items from a different table which also has a date column. So I want to do something like the following:

SELECT * FROM TABLE2 WHERE date BETWEEN fromdate AND todate

except the fromdate and todate columns are from table1, whereas 'date' is from table2. Is there a succinct way to do this?

Was it helpful?

Solution

You would do some kind of join between the the two tables and select entries from the join. A simple cartesian join can be achieved by comma-separating the tables you are querying from.

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top