Merijndk
Merijndk

Reputation: 1693

mysql select where left join syntax

I have a problem. I have 2 database tables.

table 1 people:

+----------+--------------+
|  id      | name         | 
+----------+--------------+
| 1        | johanalj     | 
| 2        | hjgjhggjh    | 
+----------+--------------+  

table 2 images of people:

+----------+--------------+----------------+
|  id      | url          | people_ID      |
+----------+--------------+----------------+
| 1        | 3765345.png  | 1              |
| 2        | 87e58974.png | 1              |
+----------+--------------+----------------+  

Now I want to select person with id 1 from table 1 and all pictures from table 2 that have people_ID 1.

I tried LEFT JOIN in combination with a WHERE but cant get it to work

$sql = "SELECT * FROM people p LEFT JOIN images i  ON  i.people_ID = p.id WHERE id = '1'";

But I get a no result massage. What am I doing wrong?

Upvotes: 1

Views: 522

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31749

There is an error(ambiguous column id). Both tables have id column. You need to add the table alias with id. try with -

$sql = "SELECT * FROM people p LEFT JOIN images i  ON  i.people_ID = p.id WHERE p.id = '1'";

Upvotes: 6

Related Questions