Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL LEFT JOIN with optional value in second table

Tags:

sql

join

mysql

I have two tables: projects and user_licenses.

I'd like to grab the entire list of projects from the database, as well as the user's current license state. The licenses table has a user ID field which I check against a $_SESSION variable for the value. The thing is, they might not have a license, or a non-logged in visitor may want to see the projects list. My question is this: How can I get the data from the left table always display, but only grab data for that row from the right table when certain conditions are met?

The query I have at the moment is this:

   SELECT Projects.*, 
          UserLicenses.* 
     FROM Projects 
LEFT JOIN UserLicenses ON Projects.id = UserLicenses.project_id 
    WHERE UserLicenses.user_id = 12 
 ORDER BY name ASC
like image 718
Bojangles Avatar asked Mar 12 '11 19:03

Bojangles


People also ask

How do I join two tables without conditions?

Using the “FROM Table1, Table2” Syntax One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.

Does LEFT join accept NULL values?

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.

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. If there's no matching row, return null.

Can we use LEFT join without on condition?

You can indeed implement this using LEFT JOIN . For LEFT JOIN you must have ON but you can use ON TRUE .


3 Answers

Add any extra conditions to the on clause of the left join. They will only affect the joined table.

  SELECT Projects.*, 
          UserLicenses.* 
     FROM Projects 
LEFT JOIN UserLicenses 
       ON Projects.id = UserLicenses.project_id 
          and UserLicenses.user_id = 12 
          and UserLicences.Question = '6*7'
          and UserLicences.Answer = 42
 ORDER BY name ASC

This will return projects without matching licenses.

like image 181
Andomar Avatar answered Oct 19 '22 05:10

Andomar


Move the UserLicenses condition away from WHERE, and up to the JOIN condition. By having it in the WHERE part, you will never see those "left" rows because they are filtered away.

You can also probably use WHERE (UserLicenses.user_id = 12 OR UserLicenses.user_id IS NULL) Don't do that. Just move it to the join condition like this:

LEFT JOIN UserLicenses ON (Projects.id = UserLicenses.project_id AND UserLicenses.user_id = 12)

like image 3
cairnz Avatar answered Oct 19 '22 06:10

cairnz


You can use LEFT JOIN

If its conditions match then values show otherwise null value shows.

like image 1
Rana Hyder Avatar answered Oct 19 '22 05:10

Rana Hyder