2017-08-17 74 views
1

我有兩個表。SQL JOIN和第二個表中的一些條件的限制結果

投資組合:

+----+-------+--------------+ 
| id | name | created_date | 
+----+-------+--------------+ 
| 1 | Port 1| 2017/08/12 | 
+----+-------+--------------+ 
| 2 | Port 2| 2017/08/14 | 
+----+-------+--------------+ 
| 3 | Port 3| 2017/08/15 | 
+----+-------+--------------+ 

照片:

+----+------------+--------------+--------------+-----------------+ 
| id | Port_name | port_id    | user_id    | created_at  | 
+----+------------+--------------+--------------+-----------------+ 
| 1 | Port 1  |   1 |  null | 2017/08/10 | 
+----+------------+--------------+--------------+-----------------+ 
| 2 | Port 2  |   2 |  null | 2017/08/11 | 
+----+------------+--------------+--------------+-----------------+ 
| 3 | Port 3  |   3 |  null | 2017/08/12 | 
+----+------------+--------------+--------------+-----------------+ 
| 4 | Port 1  |   1 |   1 | 2017/08/13 | 
+----+------------+--------------+--------------+-----------------+ 
| 5 | Port 2  |   2 |   1 | 2017/08/14 | 
+----+------------+--------------+--------------+-----------------+ 
| 6 | Port 3  |   3 |   1 | 2017/08/15 | 
+----+------------+--------------+--------------+-----------------+ 
| 7 | Port 2  |   2 |   1 | 2017/08/16 | 
+----+------------+--------------+--------------+-----------------+ 
| 8 | Port 3  |   3 |   1 | 2017/08/17 | 
+----+------------+--------------+--------------+-----------------+ 
| 9 | Port 2  |   2 |   1 | 2017/08/18 | 
+----+------------+--------------+--------------+-----------------+ 
|10 | Port 3  |   3 |   1 | 2017/08/19 | 
+----+------------+--------------+--------------+-----------------+ 

我怎樣才能查詢得到結果如下圖所示:

結果:

+---------+------------+----------------+-----------------+ 
| Port_id | port_name | photo_id  | photo_created_at| 
+---------+------------+----------------+-----------------+ 
| 2  | Port 2  |  7  | 2017/08/16 | 
+---------+------------+----------------+-----------------+ 
| 3  | Port 3  |  8  | 2017/08/17 | 
+---------+------------+----------------+-----------------+ 

我想要做的是加入投資組合照片表,並照片表的一些條件限制的結果:

  1. user_id是不是空
  2. created_at是「第二古老」

「第二最老的」 含義是,例如,我有4個日期時間: 2017/08/112017/08/122017/08/162017/08/19

在這種情況下,「第二最老」是2017/08/12

我已經嘗試過自己:

SELECT p.id as Port_id, 
     p.name as Port_name, 
     ph.id as photo_id, 
     ph.created_at as photo_created_at 
FROM "Portfolios" AS p 
LEFT JOIN (
      SELECT * 
      FROM "Photos" 
      WHERE user_id IS NOT NULL 
      ORDER BY created_at ASC 
      LIMIT 1 OFFSET 1) as ph 
ON p.id = ph.port_id; 

,並用Google搜索這一點,但發現沒有解決我的問題。

任何幫助? 感謝您的進步!

+0

出在你的例子這四個日期,怎麼是'2017年/ 08/12'「半最老」?你的意思是「第二古老」? – user2877959

+0

@ user2877959是的,我的意思是「第二古老」。 – javimuu

+0

好的。但我仍然不明白你想要的結果。爲什麼沒有進入組合1?照片#7是投資組合2中第二古老的照片?它不應該是#5嗎?投資組合3同樣適用... – user2877959

回答

1

我已經使用了兩個GROUP BY構造。我省略了Portfolios表,因爲它似乎並不重要

select photos.port_id, min(photos.id), min(photos.created_at) 
from photos 
join 
(
    select port_id, min(created_at) photos_ca_min 
    from photos 
    where user_id is not null 
    group by port_id 
) p on p.port_id = photos.port_id 
where photos.created_at > p.photos_ca_min and user_id is not null 
group by photos.port_id 

SQLFiddle

1

像拉迪姆,我不明白,需要加入到投資組合,因爲這無助。我的解決方案使用ROW_NUMBER()函數:

SELECT port_id, port_name, id as photo_id, created_at as photo_created_at FROM 
(select *, row_number() OVER (PARTITION BY port_id ORDER By created_at) AS rn from 
photos WHERE user_id IS NOT NULL) r 
WHERE r.rn = 2 

然而,就感鏈接到投資組合如果照片不重複port_name中指!理論上講,port_name不應該在照片中!

編輯

如果您從照片port_name中指那麼你需要:

SELECT r.port_id, p.port_name, r.id as photo_id, r.created_at as photo_created_at FROM 
(select *, row_number() OVER (PARTITION BY port_id ORDER By created_at) AS rn from 
photos WHERE user_id IS NOT NULL) r INNER JOIN portfolios p on p.id = r.port_id 
WHERE r.rn = 2 
+0

你說得對。 Port_name不應在照片中。 – javimuu

+0

看到我的編輯port_name從照片中刪除 –

+0

非常感謝你! – javimuu