我想加入两张桌子。表1称为account,它由account\u id、fname、lname、picture组成,表2称为friendpending,由account\u id、friend\u id和message组成。我正在制作一个允许用户添加好友的程序,当我传入帐户id时,它将返回该用户名,我需要它返回该好友id名称。
这是我迄今为止所做的尝试:

SELECT account.fname, account.lname, account.picture
from account
INNER JOIN friendpending ON account.account_id = friendpending.account_id
WHERE friendpending.account_id = p_account_id;

最佳答案

如果要从friendpending表中选择朋友的姓名,则需要选择该字段(或多个字段):

SELECT friendpending.name,
    account.fname, account.lname, account.picture
FROM account INNER JOIN friendpending ON account.account_id = friendpending.account_id
WHERE friendpending.account_id = p_account_id;


SELECT friendpending.fname, friendpending.lname, account.picture
FROM account INNER JOIN friendpending ON account.account_id = friendpending.account_id
WHERE friendpending.account_id = p_account_id;

或者,根据下面的评论,ON account.account_id = friendpending.friend_id

关于php - SQL Join返回错误结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47814712/

10-15 07:04