Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - INNER JOIN two tables with a LIMIT

Tags:

sql

postgresql

I've seen this post which almost coincides with my question but my specific problem is that I need to put a limit to the third table/query, as in LIMIT 15, for example. Is there an easy way to achieve this? Thanks!

EDIT

My SQL SELECT statement would look something like this:

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'

To clarify: It's just a matter of joining two tables but the second table has two states. First state as a "common user" and the next state as an "artist" (both using the same table, e.g. users).

like image 991
MoMo Avatar asked Jun 03 '13 08:06

MoMo


1 Answers

Try this query:

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
like image 80
Pritesh Tayade Avatar answered Sep 17 '22 18:09

Pritesh Tayade