我嘗試加入使用一個查詢與列名=>列值的子陣列表..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和不可能維護查詢。 –