Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return only one row from the right-most table for every row in the left-most table

Tags:

sql

join

mysql

I have two tables. I want to join them in a way that only one record in the right table is returned for each record in the left most table. I've included an example below. I'd like to avoid subqueries and temporary tables as the actual data is about 4M rows. I also don't care which record in the rightmost table is matched, as long as one or none is matched. Thanks!

table users:

------------- | id | name | ------------- | 1  | mike | | 2  | john | | 3  | bill | ------------- 

table transactions:

--------------- | uid | spent |  --------------- | 1   | 5.00  | | 1   | 5.00  | | 2   | 5.00  | | 3   | 5.00  | | 3   | 10.00 | --------------- 

expected output:

--------------------- | id | name | spent | --------------------- | 1  | mike | 5.00  | | 2  | john | 5.00  | | 3  | bill | 5.00  | --------------------- 
like image 817
Mike Sherov Avatar asked Jul 22 '10 21:07

Mike Sherov


People also ask

Which returns all the rows from the right table with the matching rows in the left table?

Introduction to SQL Server LEFT JOIN clause The LEFT JOIN clause allows you to query data from multiple tables. The LEFT JOIN returns all rows from the left table and the matching rows from the right table.

How do I return a row from left table to right table?

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.

How do I return only one row in SQL?

To return only the first row that matches your SELECT query, you need to add the LIMIT clause to your SELECT statement. The LIMIT clause is used to control the number of rows returned by your query. When you add LIMIT 1 to the SELECT statement, then only one row will be returned.

What returns rows only when there is at least one row from both the table that matches the join condition?

Introduction to SQL LEFT JOIN clause In the previous tutorial, you learned about the inner join that returns rows if there is, at least, one row in both tables that matches the join condition. The inner join clause eliminates the rows that do not match with a row of the other table.


2 Answers

Use:

  SELECT u.id,          u.name,          MIN(t.spent) AS spent     FROM USERS u     JOIN TRANSACTIONS t ON t.uid = u.id GROUP BY u.id, u.name 

Mind that this will only return users who have at least one TRANSACTIONS record. If you want to see users who don't have supporting records as well as those who do - use:

   SELECT u.id,           u.name,           COALESCE(MIN(t.spent), 0) AS spent      FROM USERS u LEFT JOIN TRANSACTIONS t ON t.uid = u.id  GROUP BY u.id, u.name 
like image 122
OMG Ponies Avatar answered Sep 27 '22 21:09

OMG Ponies


If you do not care about the particular row that you get back.

select id, name, spent from users left join transactions on users.id = transactions.uid group by id 

This will return one row per user. It will be the first matched transaction.

like image 38
TheJacobTaylor Avatar answered Sep 27 '22 20:09

TheJacobTaylor