简体   繁体   English

如何从MySQL到PHP以一对多关系处理数据

[英]How to process data with a one to many relation from MySQL into PHP

I am using the following query to return data about a single habit. 我正在使用以下查询返回有关单个习惯的数据。

A habit can have many steps and many comments. 一个习惯可以有很多步骤和很多注释。

When I run the following query it repeats the data for the habit for every step and comment there is. 当我运行以下查询时,它会针对每一步重复该习惯数据并进行注释。

        SELECT 
        habit_id, habit_user_id, habit_name, habit_category_id, habit_description, habit_target_user, habit_up_votes, habit_down_votes, 
        step_id, step_description, 
        comment_id, comment_user_id, comment_description
        FROM habits, steps, comments 
        WHERE habit_id = ? 
        AND habit_id = step_habit_id
        AND habit_id = comment_habit_id
        ORDER BY step_order

Example output (the ids should be enough to get an idea of what is going on): 输出示例(ID应该足以了解发生了什么):

habit_id    step_id    step_description    comment_id    comment_description
1           1          do x                1             this works great!
1           1          do x                2             Awful!
1           1          do x                3             nice job
1           2          then do y           1             this works great!
1           2          then do y           2             Awful!
1           2          then do y           3             nice job

I want to be able to take this returned data and have it in one array. 我希望能够获取此返回的数据并将其放在一个数组中。

array("habit_id" => 1, "step_id" => array(1, 2), "comment_id" => array(1, 2, 3));

The only ways I can think of doing this are by either: 我可以想到的唯一方法是:

Executing 3 seperate queries 1 to get the habit data, 1 to get the steps for that habit and 1 to get the comments for that habit. 执行3个单独的查询1来获取习惯数据,1来获取该习惯的步骤,1来获取该习惯的注释。

or 要么

By using the above query as is and constructing a new array with the habit data and then looping through all the rows and constructing a new array for the steps and for the comments while at the same time making sure there are no duplicates added. 通过按原样使用上述查询并使用惯常数据构造一个新数组,然后循环遍历所有行,并为步骤和注释构造一个新数组,同时确保没有添加重复项。

This sounds way too inefficient, can anyone suggest a better way either by modifying my query to provide PHP with more workable data or with some trick in PHP. 这听起来效率太低,任何人都可以提出一种更好的方法,方法是修改我的查询以为PHP提供更多可行的数据,或者提供一些PHP技巧。 I did at one point consider concatenating the data into arrays for the steps and comments within the query, but I thought this should be PHP's job to manipulate the data in such a way. 我曾经考虑过将数据连接到数组中以进行查询中的步骤和注释,但是我认为这应该是PHP的工作,以这种方式来操作数据。

The reason why I want my data in this way is to return it to be used in an Angularjs application. 我希望以这种方式获取数据的原因是将其返回以在Angularjs应用程序中使用。

The query is fine as is. 查询很好。 It's returning enough data for you to iterate through and build the array you want. 它返回了足够的数据供您迭代并构建所需的数组。

$data = array();

// could be any type of loop here. whatever returns data from your query
while($row=mysql_fetch_array($result)) {
    $data['habit_id'] = $row['habit_id'];

    // the in_array check ensures there are no dupes
    if (!in_array($row['step_id'], $data['step_id'])) {
        $data['step_id'][] = $row['step_id'];
    }
    if (!in_array($row['comment_id'], $data['comment_id'])) {
        $data['comment_id'][] = $row['comment_id'];
    }
}

This is going to be more efficient than three separate queries. 这将比三个单独的查询更有效率。 Make sure your database is properly indexed. 确保数据库已正确索引。 And check that you're using those indexes by running your query with EXPLAIN. 并通过使用EXPLAIN运行查询来检查您是否正在使用那些索引。

I would do this way. 我会这样做。 let me know if this solve your problems. 让我知道这是否可以解决您的问题。

<?php

$test = NULL;
$test['habit_id']= $habitID; // I assume $habit is known;
$query = "SELECT * FROM steps
        WHERE habit_id = '".$habitID."';";
$result= mysql_query($query, $con);
while($row=mysql_fetch_array($result))
    $test['step_id'][]=$row['step_id'];

$query= "SELECT * FROM comments 
        WHERE habit_id = '".$habitID."';";
$result= mysql_query($query, $con);
while($row=mysql_fetch_array($result))
    $test['comment_id'][]=$row['comment_id'];

var_dump($test);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM