SQL JOIN 并限制第二个表中某些条件的结果

标签 sql postgresql

我有两个表。

投资组合:

+----+-------+--------------+
| 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   |
+---------+------------+----------------+-----------------+

我想做的是加入 Portfolios 表和 Photos 表,并在某些条件下限制 Photos 表的结果:

  1. user_id 不为空
  2. created_at 是“第二老”

“第二老”的意思是,例如,我有 4 个日期时间: 2017/08/11, 2017/08/12, 2017/08/16, 2017/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;

并用谷歌搜索,但没有找到解决我的问题的方法。

有什么帮助吗? 提前致谢!

最佳答案

我使用了两个 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

关于SQL JOIN 并限制第二个表中某些条件的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45730225/

相关文章:

mysql - 无法添加外键约束为主外键

json - postgres 9.4 和 json 中的 unicode

sql - 找到正确的 'record' 并仅使用该记录中的数据

sql - Mysqldump 命令出错

sql - 左外连接查询

excel - PostgreSQL 查询输出为 Excel 文件

mysql - 我想找到上升一、下降一的情况。使用SQL

mysql - 连接/合并两个表,即兴/组成 "missing"条目

ruby-on-rails - 如何调整在使用生产级 Heroku Postgres 的 Heroku 上运行的 Ruby on Rails 应用程序?

database - 从 pg_dumpall : relation does not exist, 无效命令恢复 Postgres\N