如何从不同的表中回显值?

时间:2013-08-25 01:27:09

标签: php mysql

这是我想要实现的一个非常简单的例子:

USERS
 id      |    name          
 12      |    Phil      

ACTIONS
 sender  |  recipient
 12      |  Alice

$table = query("SELECT id, sender FROM users WHERE recipient = Alice");
foreach ($table as $row) {
$sender = $row['sender'];

echo '$sender has sent a gift to Alice';
}

这将输出:

12 sent a gift to Alice

如何获得以下输出?

**Phil** sent a gift to Alice

我应该加入这两张桌子吗?

1 个答案:

答案 0 :(得分:1)

您需要加入:

SELECT u.name, sender
FROM actions a join
     users u
     on a.sender = u.id
WHERE recipient = 'Alice';