简体   繁体   English

PHP / MySQL-选择表1中的列等于表2中的列的所有行

[英]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.

相关问题 MySQL / PHP-选择表1中的列等于表2中的列的所有行,并创建一个foreach循环 - MySQL/PHP - Select all the rows where column from table 1 is equal to column from table 2 and creating a foreach loop SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL) PHP MySQL:从表A获取行,其中表B的Z列中不存在表A的Z列的值 - PHP MySQL: Getting rows from table A where values of column Z of Table A not present in column Z of table B PHP从MySQL表中选择并返回包含最多行的列 - PHP Select from MySQL table and return column with most rows mysql:从表中选择键等于一个值的表,并直接或间接选择与该值相关的所有行 - mysql: select from a table where key is equal to a value and also select all the rows related to that value directly or indirectly PHP / MySQL从表中选择所有行,并显示具有最低值的列值 - PHP/MySQL Select all rows from table and display the column value with lowest value php 中 mysql 表中的 Select 计数(列) - Select count(column) from table of mysql in php 如何从MySQL表中选择日期和日期之间的所有列 - How to select all from MySQL table where column between dates and order by column 从表中选择,其中列a不等于列b(在同一表上)且tableID = 2 - select from table where column a is not equal column b (on same table) and tableID = 2 Mysql返回表的所有值,其中列等于用户输入 - Mysql Return all values of a table where column is equal to user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM