如何使用PHP转换标准化数据集?

如何使用PHP转换标准化数据集?,php,mysql,pivot,normalization,Php,Mysql,Pivot,Normalization,这是我的桌子: 用户表 id | name 1 | john 2 | lucy 用户帐户表 id | user_id | account_name 1 2 lucy_lu 2 1 johndoe 帐户参数表 id | account_id | parameter_id | value 1 1

这是我的桌子:

用户表

id   |   name  
1    |   john  
2    |   lucy  
用户帐户表

id   |   user_id    |   account_name
1          2             lucy_lu
2          1             johndoe
帐户参数表

id   |  account_id  |  parameter_id   |  value
1           1              10             4000
2           1              11             1450
3           2              10             5000
4           2              11             1150
id   |   parameter_value  |  parameter_name
10          height               Height
11          max_score            Max Score
参数表

id   |  account_id  |  parameter_id   |  value
1           1              10             4000
2           1              11             1450
3           2              10             5000
4           2              11             1150
id   |   parameter_value  |  parameter_name
10          height               Height
11          max_score            Max Score
我想得到结果:

user_id | user_name | account_name |  Height  |  Max Score
1          john        johndoe        5000       1150
2          lucy        lucy_lu        4000       1450
我搜索了“mysql使用数据生成列”和类似的内容,找到了许多针对1或2个表的解决方案,但我不知道如何在这里实现这些解决方案。我知道如何选择带有连接的静态字段,但我不知道在本例中如何选择“高度”和“最大分数”。

如果必须使用SQL来执行此操作,那么使用编写SQL的PHP似乎是最不紧密耦合的方法

首先,查询参数表并存储在数组中

然后将查询字符串分为三部分,中间部分是动态创建的

SELECT
    user_accounts.user_id          AS user_id,
    user.name                      AS user_name,
    user_accounts.account_name     AS account_name,

对数组中的每条记录(来自
参数
)重复此操作

使用PHP替换@param_id和@param_name值

(小心逗号,不要在SQL的最后一行使用逗号。)


注意:在MySQL中,您只需要
按user\u accounts.user\u id
分组,但我不建议一般滥用该功能


执行动态构建的查询应该为您的结果提供数据透视


您可能需要一些上下文敏感度来确定哪些参数可以包含在最终结果中,哪些参数不可以包含在最终结果中(使用这种结构,您可能并不总是需要所有参数)。

由于我做了一些类似的事情,这里有一些事情要开始:

$parameters = array(
       array("id" => 10, "parameter_value" => "height", "parameter_name" => "Height"),
       array("id" => 11, "parameter_value" => "max_score", "parameter_name" => "Max Score")
); //fetch all parameters from your parameters table

//specify the table where the values are stored
$tableName = 'account_parameters';
//specify the column name in this table where the identifier for your data lies
$dataIdColumnName = 'account_id';

//set the first column up, which holds your datakey
$selectPivotFields = array("`".$tableName."`.`".$dataIdColumnName."`");

//loop over all parameters  
if(is_array($parameters))
    for($i=0;$i<count($parameters);$i++)
    {
        //build a part of your pivot query
        $selectPivotFields[] = sprintf("MAX(IF(`%1$s`.`parameter_id` = '%2$d', `%1$s`.`value`, NULL)) AS `%3$s`", $tableName, $parameters[$i]['id'], $parameters[$i]['parameter_value']);
    }

    //build the actual query for readability i simplified it a little bit
    $sql = sprintf("
              SELECT
                %1$s
               FROM
                `%2$s`
               WHERE
                `%2$s`.`%3$s` = '%4$d'
               GROUP BY
                `%2$s`.`%3$s`
              ", implode(", ", $selectPivotFields), $tableName, $dataIdColumnName, $dataId);

     //and execute your query with the prefered class
     $result = execute($sql);
$parameters=数组(
数组(“id”=>10,“参数值”=>“高度”,“参数名称”=>“高度”),
数组(“id”=>11,“参数值”=>“最大分数”,“参数名称”=>“最大分数”)
); //从参数表中获取所有参数
//指定存储值的表
$tableName='account_parameters';
//在此表中指定数据标识符所在的列名
$dataIdColumnName='account_id';
//设置第一列,它保存您的数据键
$selectPivotFields=array(“`.$tableName.”.“`.$dataIdColumnName.”);
//循环所有参数
if(is_数组($parameters))

对于($i=0;$i分组依据是什么?我认为用户id 1的高度应该是4000和其他数据too@AnkitBajpai-
account\u id=1
映射到
user\u id=2
映射到
name=lucy
=>lucy是user\u id 2,高度为4000。@Alex,是的,PHP(laravel)在这种情况下..@MatBailie我是这样想的,但仍然在考虑使用特定的PHP方法…@wast-然后将其标记为PHP,并询问如何使用PHP转换规范化数据集?这是可行的。但是,我使用了MAX而不是SUM。因为使用SUM我无法获得字符串值。谢谢。