David David - 5 months ago 11
SQL Question

Do able to add WHERE in JOIN query

I'm using the following query in my PHP-Code:

SELECT
tables.city,
tables.class,
tables.street,
tables.number,
customers.hyperlink

FROM
tables

JOIN
customers

ON
tables.id = customers.id


I'm not able to add WHERE. I always get an error. I want to

SELECT
tables.city,
tables.class,
tables.street,
tables.number

FROM tables

WHERE
city = "something"

ORDER BY = "something"


What am I doing wrong?

Answer

The order by must contain the columns name and not the values and in join (if ambigous) you must explicit the tablename for column also in where clause

    SELECT
    tables.city,
    tables.class,
    tables.street,
    tables.number,
    customers.hyperlink
    FROM  tables
    JOIN  customers     ON tables.id = customers.id
    WHERE tables.city = "something"
    ORDER BY tables.city