Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit results of a LEFT JOIN

Tags:

Take the case of two tables: tbl_product and tbl_transaction.
tbl_product lists product details including names and ids while tbl_transaction lists transactions involving the products and includes dates, product-ids, customers etc.

I need to display a web-page showing 10 products and for each product, the last 5 transactions. So far, no LEFT JOIN query seems to work and the subquery below would have worked if mysql could allow the tx.product_id=ta.product_id part (fails with Unknown column 'ta.product_id' in 'where clause': [ERROR:1054]).

SELECT   ta.product_id,   ta.product_name,   tb.transaction_date   FROM tbl_product ta   LEFT JOIN (SELECT tx.transaction_date FROM tbl_transaction tx WHERE tx.product_id=ta.product_id LIMIT 5) tb LIMIT 10 

Is there a way to achieve the listing I need without using multiple queries in a loop?

Edit:
This is exactly what I need from MySQL:

SELECT ta.product_id, ta.product_name, tb.transaction_date ...   FROM tbl_product ta   LEFT JOIN tbl_transaction tb ON (tb.product_id=ta.product_id LIMIT 5)   LIMIT 10 

Of course this is illegal, but I really wish it wasn't!

like image 924
eddy edu Avatar asked Jul 27 '10 23:07

eddy edu


People also ask

How do you reduce left join in SQL Server?

WITH myquery AS ( SELECT * FROM TEST_FILE1 ) , a as ( select col1, col2, 42 as col3, col4, col5 from ( (select distinct col1 from myquery) cross join (select distinct col2 from myquery) cross join (select distinct col4 from myquery) cross join (select distinct col5 from myquery) ) ) select a.

What does (+) mean in SQL joins?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key.

What is the result of left join?

The LEFT JOIN command returns all rows from the left table, and the matching rows from the right table. The result is NULL from the right side, if there is no match.

Does LEFT join return all rows?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.


1 Answers

This is where ranking functions would be very useful. Unfortunately, MySQL does not yet support them. Instead, you can try something like the following.

Select ta.product_id, ta.product_name     , tb.transaction_date From tbl_product As ta     Left Join   (                 Select tx1.product_id, tx1.transaction_id, tx1.transaction_date                     , (Select Count(*)                         From tbl_transaction As tx2                         Where tx2.product_id = tx1.product_id                             And tx2.transaction_id < tx1.transaction_id) As [Rank]                 From tbl_transaction As tx1                 ) as tb         On tb.product_id = ta.product_id             And tb.[rank] <= 4 Limit 10 
like image 179
Thomas Avatar answered Dec 23 '22 10:12

Thomas