Marilee
Marilee

Reputation: 1598

Mysql inner join error

I have the following problem, im trying to execute and learn about joins as stipulated in W3schools here

Im trying to use a join to get data from 2 tables. Im using the following query:

SELECT rentals.*, cars.tank_capacity, cars.price FROM rentals 
                WHERE mvc_nr = '$mvc' AND active = 'y'
                INNER JOIN cars
                ON cars.mvc_nr = rentals.mvc_nr";

both column mvc_nr and active exists in both tables and values correspond.

The value of $mvc = 123456789 in both tables

When I run the query without the WHERE clause I get all the data back and the query executes successfully, but as soon as I add that WHERE clause I get the error

The error reads: syntax error near 'INNER JOIN cars ON cars.mvc_nr = rentals.mvc_nr'

Any help appreciated

Upvotes: 0

Views: 211

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93694

Where clause needs to added after all the JOIN's

SELECT rentals.*, cars.tank_capacity, cars.price 
FROM rentals 
INNER JOIN cars
        ON cars.mvc_nr = rentals.mvc_nr
WHERE rentals.mvc_nr = '$mvc' AND rentals.active = 'y'

Upvotes: 1

Related Questions