简体   繁体   中英

PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM