Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use an IF statement in an MySQL join query?

Tags:

sql

mysql

I have a SQL query that left joins a table in different ways depending on a condition.

SELECT m.id, u.first_name AS otherUser
FROM matches AS m
IF (u.id=m.user2ID)
    LEFT JOIN users AS u ON u.id = m.user1ID
ELSE
    LEFT JOIN users AS u ON u.id = m.user2ID
ENDIF
WHERE m.user1ID=2 OR m.user2ID=2

matches is a table with integer columns user1ID and user2ID. users is a table containing users of my web application. users has a VARCHAR field called first_name.

The intention of this query is to get the names of the users matched with the current user.

However, MySQL returns this error:

#1064 - You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for 
the right syntax to use near 'IF (u.id=m.user2ID) LEFT JOIN users 
AS u ON u.id = m.user1ID ELSE LEFT JOIN user' at line 3

Why?

like image 274
John Hoffman Avatar asked Apr 08 '12 15:04

John Hoffman


People also ask

Can we use if condition in join?

You can not use the IF THEN ELSE END IF -stuff in a SELECT in this way. However, you can use it in stored procedures and functions. I would JOIN u.id with both m. user1ID and m.

How add if condition in join?

A conditional column join is a fancy way to let us join to a single column and to two (or more) columns in a single query. We can accomplish this by using a case statement in the on clause of our join. A case statement allows us to test multiple conditions (like an if/else if/else) to produce a single value.

Can we use if statement in SELECT query in MySQL?

Learn MySQL from scratch for Data Science and AnalyticsIt is quite possible to use MySQL IF() function within SELECT statement by providing the name of the column along with a condition as the first argument of IF() function. To understand it, consider the following data from table 'Students'.

Should I put condition in join or WHERE clause?

Always put the join conditions in the ON clause if you are doing an INNER JOIN . So, do not add any WHERE conditions to the ON clause, put them in the WHERE clause. If you are doing a LEFT JOIN , add any WHERE conditions to the ON clause for the table in the right side of the join.


2 Answers

Specify the condition as part of the ON clause:

SELECT m.id, u.first_name AS otherUser
FROM matches AS m
LEFT JOIN users AS u ON (u.id=m.user2ID and u.id = m.user1ID) or (u.id<>m.user2ID and u.id = m.user2ID) 
WHERE m.user1ID=2 OR m.user2ID=2

Another way to do the same thing:

SELECT m.id, u.first_name AS otherUser
FROM matches AS m
LEFT JOIN users AS u ON IF(u.id=m.user2ID,u.id = m.user1ID,u.id = m.user2ID) 
WHERE m.user1ID=2 OR m.user2ID=2
like image 162
Dojo Avatar answered Oct 02 '22 22:10

Dojo


Use this query:

SELECT m.id, u.first_name AS otherUser FROM matches AS m LEFT JOIN users AS u ON u.id = m.user1ID AND u.id=m.user2ID LEFT JOIN users AS u1 ON u1.id = m.user2ID WHERE m.user1ID=2 OR m.user2ID=2

Updated query:

SELECT m.id, u.first_name AS otherUser
FROM matches AS m
LEFT JOIN users AS u ON u.id = (CASE WHEN u.id=m.user2ID 
                                   THEN m.user1ID
                                   ELSE m.user2ID
                               END)
WHERE m.user1ID=2 OR m.user2ID=2

Check this Source.

Also check this MYSQL Inner Join if statement . It might be helpful for you.

like image 34
Somnath Muluk Avatar answered Oct 02 '22 22:10

Somnath Muluk