我有一个表演表和一个表演表。
我正在努力寻找即将开放的节目"通过要求"节目的第一场演出在下周发生"。
以下是相关数据字段:
显示
ID
name(varchar)
...
PERFORMANCES
ID
show_id(int / fk)
date_time(datetime)
...
我认为这里需要逐个分组,但我还没有弄清楚。
任何帮助都会很棒。
谢谢!
答案 0 :(得分:0)
试试这个:
SELECT shows.name, performances.date_time AS "Opening Date"
FROM shows
INNER JOIN PERFORMANCES
ON shows.id = performances.show_id
WHERE performances.date_time <= (CURRENT_DATE + interval '1 week')
AND performances.date_time > CURRENT_DATE
样本输出 SQL Fiddle
PostgreSQL 9.3.1架构设置:
CREATE TABLE Shows
("id" int, "name" varchar(20))
;
INSERT INTO Shows
("id", "name")
VALUES
(1, 'Chicago'),
(2, 'Cats'),
(3, 'Some other show')
;
CREATE TABLE Performances
("id" int, "show_id" int, "date_time" date)
;
INSERT INTO Performances
("id", "show_id", "date_time")
VALUES
(1, 1, '2014-04-18'),
(2, 2, '2014-05-18'),
(3, 3, '2012-05-18');
;
查询1 :
SELECT shows.name, performances.date_time AS "Opening Date"
FROM shows
INNER JOIN PERFORMANCES
ON shows.id = performances.show_id
WHERE performances.date_time <= (CURRENT_DATE + interval '1 week')
AND performances.date_time > CURRENT_DATE
<强> Results 强>:
| NAME | OPENING DATE |
|---------|------------------------------|
| Chicago | April, 18 2014 00:00:00+0000 |