[英]How to select three table with same column name but different values
Hi i'm so confused to select 3 table data value.嗨,我很困惑要选择 3 个表数据值。
Ex: In table1:例如:在表 1 中:
Id value price <br>
1 101 30 <br>
2 102 40 <br>
3 103 50 <br>
In table 2在表 2
value price <br>
101 25 <br>
102 35 <br>
103 45 <br>
In table 3在表 3
value Price <br>
101 5% <br>
102 6% <br>
103 7% <br>
in this how can i get all price value with id在这种情况下,我如何获得带有 id 的所有价格值
I need a result like我需要这样的结果
ID Price Price Price <br>
1 -- 30 -- 25 -- 5%<br>
2 -- 40 -- 35 -- 6%<br>
3 -- 50 -- 45 -- 7%<br>
Try with inner join尝试使用内连接
select id,table1.value, table1.price as price1,table2.price as price2,table3.price as price3 from table1
inner join table2 on table1.value=table2.value
inner join table3 on table2.value=table3.value
SELECT table1.id , table1.price , table2.price ,table3.price
FROM table1
JOIN table2 ON table.id = table2.id
JOIN table3 ON table.value = table3.value
Result结果
select table1.id,table1.value, table1.price,table2.price,table3.price
从 table1 内连接 table2 上 table1.value=table2.value 内连接 table3 上 table2.value=table3.value
What you need to do is perform a JOIN
on your tables, using a coherent value known as primary and foreign key.您需要做的是使用称为主键和外键的一致值对表执行
JOIN
。 The coherent values that links your tables together in this case is the column named value .在这种情况下,将您的表链接在一起的一致值是名为value的列。 So that's what you need to be using in your
JOIN
所以这就是你需要在你的
JOIN
SELECT
table1.id as TABLE1_ID,
table1.price as TABLE1_PRICE,
table2.price as TABLE2_PRICE,
table3.price as TABLE3_PRICE
FROM
table1
LEFT JOIN
table2 ON table1.`value` = table2.`value`
LEFT JOIN
table3 ON table2.`value` = table3.`value`
Outputs:输出:
More about JOIN
's here .更多关于
JOIN
的信息在这里。
Since you mentioned PHP in the comment section, try something like this:既然您在评论部分提到了 PHP,请尝试以下操作:
$sql="SELECT
table1.id as TABLE1_ID,
table1.price as TABLE1_PRICE,
table2.price as TABLE2_PRICE,
table3.price as TABLE3_PRICE
FROM
table1
LEFT JOIN
table2 ON table1.`value` = table2.`value`
LEFT JOIN
table3 ON table2.`value` = table3.`value`"
$result_set=mysqli_query($conn, $sql);
$row=mysqli_fetch_array($conn, $result_set);
$rowcount=mysqli_num_rows($result_set);
$count=0;
while($rowcount > $count) {
$count++;
echo 'id: '.$row['id'].'<br />';
echo 'price: '.$row['price'].'<br />';
$row=mysqli_fetch_array($conn, $result_set);
}
Remember to include
your database connection file so that $conn
is accessible.请记住
include
您的数据库连接文件,以便可以访问$conn
。
If it doesn't work initially, try to swap out the actual column names with the defined aliases, like so:如果它最初不起作用,请尝试用定义的别名替换实际的列名,如下所示:
$row['id']
to $row['TABLE1_ID']
etc. $row['id']
到$row['TABLE1_ID']
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.