简体   繁体   English

如何将两个表列与一个表联接?

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

相关问题 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns sql连接中存在相同名称的两列时,如何从一个表列获取值 - How to get value from one table column when two columns of the same name exist in an sql join 将一列联接到另一张表 - Join in one column to another table 一列为空时如何联接两个表? - How to join two tables when one column is null? 如何将其他表中的一列与 Laravel 连接起来? - How can I join one column in other table with Laravel? 如何从两个表连接中 select 一个值? - How to select one value from a two table join? 如何将MySQL表中的两行连接到第三行 - How to join two rows from a mysql table into a third one 我们如何将这两个表合并为一个 - how can we join this two table results in to one 是否可以通过使用外键使用连接子句将两个表中的其他列名显示到一个表中 - Is it possible to display other column names from two table to one table by using join clause by using foreign key PHP连接两个表,其中一个表中的两列包含from_ID和to_ID引用另一表中的id - PHP join two table where two column in one table contains from_ID and to_ID refer to id in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM