gpt4 book ai didi

php - 同一张表输出多个左关节

转载 作者:行者123 更新时间:2023-11-28 23:57:24 24 4
gpt4 key购买 nike

所以我有一个表,其中有 2 个字段引用另一个表中的 sponsorID。我需要从其他表的两个字段中提取 nickname

accounts table
sponsorID Nickname
1 Rob
2 Mark

tables table
ID sponsorID1 sponsorID2
1 1 2

我需要使用 sponsorID1sponsorID2 提取昵称。到目前为止我所做的是

$conn = new PDO('mysql:host=localhost;dbname=helpinghands', 'root', '');
$table = $conn->query("SELECT a.l1_1 as lv1_1, a2.l2_1 as lv2_1 FROM tables LEFT JOIN accounts AS a ON tables.l1_1=a.sponsorID LEFT JOIN accounts AS a2 ON tables.l2_1=a2.sponsorID");
for ($t in $table) {
echo $nickname1 = $t['??'];
echo $nickname2 = $t['??'];
}

我不知道如何进行正确的查询。它只是向我显示一个空白页。任何帮助将不胜感激。

最佳答案

我已经让它按如下方式工作:

SELECT t.ID, a1.nickname, a2.nickname
FROM tables AS t
LEFT JOIN accounts AS a1 ON t.sponsorID1 = a1.sponsorID
LEFT JOIN accounts AS a2 ON t.sponsorID2 = a2.sponsorID;

关于php - 同一张表输出多个左关节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31258322/

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