来自两个表的MYSQL请求

时间:2012-05-17 12:06:37

标签: php mysql request database-table

可能你可以帮助我:

我有一张桌子:

1: Users
id   username   email
1    myname     myname@myname.com
2    myname2    myname2@myname.com

2: Accounts
user_id   account
1         37992054
2         3804933 
1         23286966

我需要提出请求并获取此数据:

id   username   email                account
1    myname     myname@myname.com    37992054, 23286966
2    myname2    myname2@myname.com   3804933

2 个答案:

答案 0 :(得分:4)

您需要加入两个表并使用分组构造来折叠具有相同用户ID的记录:

Select u.id, u.username, u.email, group_concat(a.account)
from users as u join accounts as a 
  on a.user_id = u.id
group by u.id;

答案 1 :(得分:0)

您需要标准的联接查询:

select u.id,u.username,u.email,a.account from Users as u, Accounts as a where u.id=a.id;