我尝试加入使用一个查询与列名=>列值的子阵列表..PHP的MySQL加入与字段名称子阵列
短表(1)“用户”结构数据:
user_id email ...
1 [email protected] ...
2 [email protected] ...
短表(2) “users_permissions” 与数据结构:
user_id plugin_enter offers_view ...
1 1 0 ...
2 1 1 ...
如果我使用经典方法 - 联接左
SELECT `uperms`.*, `u`.*
FROM (`users` as u)
LEFT JOIN `users_permissions` as uperms ON `u`.`user_id` = `uperms`.`user_id`
我得到经典的输出
[0] = array(
'user_id' => 1,
'email' => [email protected],
'plugin_enter' => 1,
'offers_view' => 0
),
[1] = array(
'user_id' => 2,
'email' => [email protected],
'plugin_enter' => 1,
'offers_view' => 1,
...
),
所有我需要的是输出到子阵就象这样:
[0] = array(
'user_id' => 1,
'email' => [email protected],
'permissions => array(
'plugin_enter' => 1,
'offers_view' => 0
),
),
...
这是可以做到使用一个查询?
表2(权限)包含大约60列。如果加入Table1只有一行,是否可以用列值CONCAT列名?
我认为这是不可能在PHP只有一个查询 – Chinito
更改您的table2到像uid,名称,价值,以避免60 colums和不可能维护查询。 –