1

我有 2 个表,无法让我的 MySQL 查询工作:

First Table users:
user_id name
------------
1       Bob
2       Alice

Second Table fields:
id unser_id field_id value
-----------------------------
1  1        4           foo
2  1        5           10
3  2        4           bar

现在我想以一种输出的方式加入表格:

[0] => (
          [user_id] => 1
          [name] => Bob
          [fields] => (
                         [4] => foo
                         [5] => 10
                      )
[1] => (
          [user_id] => 2
          [name] => Alice
          [fields] => (
                         [4] => bar
                      )
       )

非常感谢!

我试过了:

SELECT u.user_id, 
       u.name 
FROM   users as u 
INNER JOIN fields as f on u.user_id = d.user_id

但我为每个用户获得的只是第一个字段条目。

4

2 回答 2

0
select u.user_id,u.name,f.field_id,f.value from users u LEFT JOIN fields f ON u.user_id = f.unser_id
于 2013-10-21T18:23:48.090 回答
0
SELECT user_id,name,GROUP_CONCAT(value SEPARATOR ',')as field FROM users u JOIN fields f 
ON u.user_id=f.unser_id GROUP BY user_id

也许是这样的?

于 2013-10-21T18:24:23.290 回答