gpt4 book ai didi

php - 如何将两个表列连接到一个?

转载 作者:行者123 更新时间:2023-11-29 23:16:19 25 4
gpt4 key购买 nike

我有一个mysql表用户

username     name
--------- ------

u1 abc
u2 xyz
u3 mrz

另一个是交易

product     price      buyer seller
--------- ------ ------ -------

antivirus 20 u1 u3
e-book 10 u2 u1

我想显示为

product     price      buyer seller
--------- ------ ------ -------

antivirus 20 abc mrz
e-book 10 xyz abc

我的问题是如何连接这两个表?以及如何显示数据,就像我 echo as row['name'] 一样,它将为买家和卖家显示相同的名称?

最佳答案

试试这个,它会起作用:

使用内连接

 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`

输出:

enter image description here

关于php - 如何将两个表列连接到一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com