Reputation: 3308
I am searching for a way that i can select all the rows from MySQL table where column from table1 is equal to column from table 2.
Here is what i want to achieve describing it by code. I tried this but nothing happens.
SELECT * FROM `table1` WHERE `table1.id`= `table2.id` ORDER by `table1.name` ASC;
Can you give me a correct way to make this thing happen as an answer to my question.
Thanks in advance!
Upvotes: 1
Views: 254
Reputation: 1270061
Your query is missing a from
clause. More importantly, the backticks are incorrect . . . and not needed. And, the proper way is to use join
:
SELECT *
FROM table1 JOIN
table2
ON table1.id = table2.id
ORDER by table1.name ASC;
When you have an expression such as `table1.id` in backticks, then it is looking for a column name called exactly that -- with a period in the middle. It is not looking for id
in table1
. If you use backticks, then you need to include them around the table name and column name separately:
`table1`.`id`
But, I would suggest not using them and naming tables and columns so they are unnecessary. They are cumbersome to type and awkward to read.
Upvotes: 2