[英]how to get values from two tables?
I have two tables A and B , table A contains some columns and table B have some columns , there is one column same in both tables. 我有两个表A和B ,表A包含一些列,表B包含一些列,两个表中都有一个列相同。
I have a value for table B and and based on that value i have to find another column' rows value , and based on those values(which got from table B ) ,i have to find some columns rows from table A 我有一个表B的值,并基于该值我必须找到另一列的行值,并基于这些值(从表B中获得 ),我必须从表A中找到一些列行
You will need to use the INNER JOIN statement : 您将需要使用INNER JOIN语句:
Example of tables : 表格示例:
Table A : A.id, A.column1, A.column2
Table B : B.id, B.column1, B.column2, B.a_id
表A: A.id, A.column1, A.column2
表B: B.id, B.column1, B.column2, B.a_id
SQL 的SQL
SELECT * FROM A INNER JOIN B ON B.a_id = A.id
PHP 的PHP
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$sql = 'SELECT * FROM A INNER JOIN B ON B.a_id = A.id';
foreach ($dbh->query($sql) as $row) {
var_dump($row);
}
A simple join query: 一个简单的联接查询:
SELECT a.*,b.*
FROM B
INNER JOIN A
ON(A.<Same_Col> = B.<Same_Col>)
Switch a.*,b.*
to the columns you want to select. 将a.*,b.*
切换到要选择的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.