Php mysql 连接到带有字段名称的子数组

标签 php mysql join left-join concatenation

我尝试使用一个查询将表连接到列名称 => 列值的子数组中。

短表(1)“用户”结构与数据:

user_id   email        ...
1         <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e59d9d9da59d9dcb9d9d" rel="noreferrer noopener nofollow">[email protected]</a>    ...
2         <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="136a6a6a536a6a3d6a6a" rel="noreferrer noopener nofollow">[email protected]</a>    ...

包含数据的短表(2)“user_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' => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8a0a0a098a0a0f6a0a0" rel="noreferrer noopener nofollow">[email protected]</a>,
  'plugin_enter' => 1,
  'offers_view' => 0
),
[1] = array(
  'user_id' => 2,
  'email' => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f6666665f6666316666" rel="noreferrer noopener nofollow">[email protected]</a>,
  'plugin_enter' => 1,
  'offers_view' => 1,
  ...
),

我需要的只是输出到子数组中,如下所示:

[0] = array(
  'user_id' => 1,
  'email' => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e79f9f9fa79f9fc99f9f" rel="noreferrer noopener nofollow">[email protected]</a>,
  'permissions => array(
      'plugin_enter' => 1,
      'offers_view' => 0
  ),
),
...

这可以通过一个查询来完成吗?

表2(权限)包含大约60 列。如果仅连接到 Table1 一行,是否可以将列名称与列值 CONCAT?

最佳答案

MySQL 没有数组或嵌套结构,因此无法在 SQL 中执行此操作。

更改您的查询,以便为 users_permissions 中的所有字段提供一致的命名样式。然后,您可以使用 PHP 循环将其键与该模式匹配的所有数组元素收集到 permissions 数组中。

查询:

SELECT u.*, up.plugin_enter AS perm_plugin_enter, up.offers_view AS perm_offers_view, ...
FROM users AS u
JOIN users_permissions AS up ON u.user_id = up.user_id

PHP:

foreach ($all_results as &$row) {
    $permissions = array();
    foreach ($row as $key => $value) {
        if (strpos($key, 'perm_') === 0) {
            $permission[substr($key, 5)] = $value;
            unset($row[$key]);
        }
    }
    $row['permissions'] = $permissions;
}

您可以通过连接表中的所有列名称和值来完成此操作:

SELECT u.*, CONCAT_WS(',', CONCAT('plugin_enter:', plugin_enter), CONCAT('offers_view:', offers_view), ...) AS permissions
FROM users AS u
JOIN users_permissions AS up ON u.user_id = up.user_id

然后您的 PHP 代码可以使用 explode()$row['permissions'] 拆分为 name:value 对数组,然后将它们转换为 PHP 数组中的 key=>value

另一个解决方案是重新设计您的users_permissions 表:

user_id permission_type value
1       plugin_enter    1
1       offers_view     0
...
2       plugin_enter    1
2       offers_view     1
...

然后就可以查询:

SELECT u.*, GROUP_CONCAT(permission_type, ':', value) AS permission
FROM users AS u
JOIN users_permissions AS up on u.user_id = up.user_id

关于Php mysql 连接到带有字段名称的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44128227/

相关文章:

.net - 如何将 Queue(Of String) 作为字符串连接?

php - 字符串转 HTML?

java - 如何从java添加照片到mysql?

mysql - 在哪里下载 MySQLdump.ext 比 mysqldump.exe ver 5.7.1.7 更新的版本

MYSQL 一个表中的所有记录以及另一表中的最后一个连接记录

mysql - 无数的WHERE条件mysql

php - dompdf中的图像

php - 使用通配符字符串搜索目录,返回文件名,然后循环

php - 在另一个 block 内修改 Twig block

mysql - 全文搜索不适用于关键字 "one"