[英]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. 我正在寻找一种可以从MySQL表中选择所有行的方法,其中table1中的列等于表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. 您的查询缺少
from
子句。 More importantly, the backticks are incorrect . 更重要的是,反引号是不正确的。 .
。 .
。 and not needed.
并且不需要。 And, the proper way is to use
join
: 并且,正确的方法是使用
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. 当您在反引号中有一个像“ table1.id”这样的表达式时,它会寻找一个正好叫该名称的列名-中间有一个句点。 It is not looking for
id
in table1
. 它不在
table1
寻找id
。 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.
它们输入起来很麻烦,阅读起来很笨拙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.