[英]How to join two table column with one?
I have a mysql table user 我有一个mysql表用户
username name
--------- ------
u1 abc
u2 xyz
u3 mrz
and another is trading 另一个正在交易
product price buyer seller
--------- ------ ------ -------
antivirus 20 u1 u3
e-book 10 u2 u1
I want to show as 我想显示为
product price buyer seller
--------- ------ ------ -------
antivirus 20 abc mrz
e-book 10 xyz abc
My question is how to join these two tables ?and how to show data as if i echo as row['name'] it will show the same name both for buyer and seller ? 我的问题是如何连接这两个表?如何显示数据,就像我作为row ['name']回显的一样,它将为买卖双方显示相同的名称?
Try this it will work : 试试这个将起作用:
Use Inner Join 使用内部联接
SELECT t1.`product` AS `product`,t1.`price` AS `price`,t2.`name` AS `buyer`,t3.`name` AS `seller` FROM trading t1
JOIN user t2 ON t2.`username`=t1.`buyer`
JOIN user t3 ON t3.`username`=t1.`seller`
Output : 输出:
I ll answer here at your last reply as it won't indent properly. 我会在您最后一次答复时在这里回答,因为它无法正确缩进。
$result=mysql_query($query); // get the query
if(!$result){
// if fails, do your things..
}
while($row=mysql_fetch_assoc($result)){
// get data, i.e: print it ALL ROWS!
echo $row['seller'];
echo $row['price'];
// and more...
}
mysql_free_result($result); // free the resource.
If i did not misunderstand you, that way you can work with them values. 如果我没有误会你,那你就可以使用他们的价值观。
选择t.product AS产品,t.price AS价格,u1.name AS买方,u2.name AS卖方从交易t JOIN用户u1 ON(t.buyer = u1.username)JOIN用户u2 ON(t.buyer = u2 。用户名);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.