user1260310
user1260310

Reputation: 2227

MYSQL join query syntax

I am trying to join three tables. The first is a table of groups, the second of joiners and the third of users. Logic is I start out with id of group, then join group on joiners by groupid. Finally since I want names as well as ids I pull them from users table by joining users on userid. But alas not working. MYSQL is throwing an error saying "error near 'groups' g which usually means right before that. What am I missing?

groups id | name |userid

joiners id | groupid | userid

users id | firstname

$sql = "SELECT g.*,j.userid,u.firstname,u.id
FROM 'groups' g
LEFT JOIN 'joiners' j
ON g.id = j.groupid
LEFT JOIN 'users' u
ON j.userid = u.id
WHERE g.id = 22";

Upvotes: 0

Views: 852

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173562

Ambiguous terms such as tables and columns should be escaped using back ticks:

$sql = "SELECT g.*,j.userid,u.firstname,u.id
FROM `groups` g
LEFT JOIN `joiners` j ON g.id = j.groupid
LEFT JOIN `users` u ON j.userid = u.id
WHERE g.id = 22

This is assuming the rest of your query is correct, because I don't see the end of your string ;-)

Upvotes: 2

Related Questions