Andrew
Andrew

Reputation: 123

LEFT JOIN and WHERE causing error

I have a nested mysql_query.

$resultSub = mysql_query("SELECT * 
                            FROM ensembles 
                           WHERE en_name = $name 
                       LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID 
                       LEFT JOIN students on ensembles.en_stu = students.s_ID 
                       LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID 
                        ORDER BY $sort $orderBy");

The query works fine without the WHERE clause, which I thought may be filtering out rows for the LEFT JOIN command, but that's not the case.

Upvotes: 1

Views: 136

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Well, you put the WHERE clause in the wrong place.

Read the documentation.

Upvotes: 0

Steve
Steve

Reputation: 8809

The WHERE clause should be placed after the LEFT JOINs:

$resultSub = mysql_query("SELECT * 
                            FROM ensembles 
                       LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID 
                       LEFT JOIN students on ensembles.en_stu = students.s_ID 
                       LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID 
                           WHERE en_name = $name 
                        ORDER BY $sort $orderBy");

Upvotes: 9

Related Questions