所以我有一个表,其中有2个字段引用另一个表中的sponsorID
。我需要从两个字段中提取其他表中的nickname
。
accounts table
sponsorID Nickname
1 Rob
2 Mark
tables table
ID sponsorID1 sponsorID2
1 1 2
我需要使用sponsorID1
和sponsorID2
取出昵称。到目前为止我做了什么
$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['??'];
}
我不知道如何进行正确的查询。它只是向我显示一个空白页面。任何帮助将不胜感激。
答案 0 :(得分:1)
我的工作方式如下:
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;
答案 1 :(得分:0)
所以我得到了它的工作,我只是改变了行
$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");
要
$table = $conn->query("SELECT a.nickname as lv1_1, a2.nickname 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");
我忘了将nickname
字段放到查询中。