我有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
但我为每个用户提供了第一个字段条目。
答案 0 :(得分: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
答案 1 :(得分: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
也许是这样的?