9

我看过这篇几乎与我的问题一致的帖子,但我的具体问题是我需要对第三个表/查询进行限制LIMIT 15,例如。有没有简单的方法来实现这一目标?谢谢!

编辑

我的 SQL SELECT 语句看起来像这样:

SELECT t2.name AS user_name, t3.name AS artist_name
FROM tbl1 t1
    INNER JOIN tbl2 t2 ON t1.t1able_id = t2.id
    INNER JOIN (SELECT * FROM tbl3 WHERE artist_id = 100 limit 15) t3
        ON t2.id = t3.artist_id
WHERE t1.kind = 'kind'

澄清一下:这只是加入两个表的问题,但第二个表有两个状态。第一个状态为“普通用户”,下一个状态为“艺术家”(两者都使用同一张表,例如users)。

4

2 回答 2

6

试试这个查询:

select *
from
    tableA a
        inner join
    tableB b
        on a.common = b.common
        inner join 
    (select * from tableC order by some_column limit 15) c
        on b.common = c.common
于 2013-06-03T08:46:11.573 回答
0

几天前,我搜索了这样的答案,Postgresql 提供了使用rank()顺利完成它。我的目标是在商业公司开始其第一次破产程序之前获得最后 3 份提交的年度报告文件。

SELECT * FROM
(
    SELECT r.id, f.id AS id_file, f.file_date, min(p.process_started) AS "first_process_started",
    rank() OVER (PARTITION BY r.id ORDER BY f.file_date DESC) AS "rank"
    FROM registry r
      INNER JOIN files f ON (r.id = f.id_registry)
      INNER JOIN processes p ON (r.id = p.id_registry)
    WHERE 
      r.type = 'LIMITED_LIABILITY_COMPANY'
      AND f.file_type = 'ANNUAL_REPORT')
      AND p.process_type = ('INSOLVENCY')
    GROUP BY r.id, f.id, f.file_date
    HAVING f.file_date <= min(p.process_started)
) AS ranked_files
WHERE rank <= 3
于 2020-03-17T10:15:07.800 回答