简体   繁体   English

多个MySQL查询还是一个具有多个JOIN的查询?

[英]Multiple MySQL queries or one with several JOINs?

Normally the obvious answer to me would be "several joins." 通常,对我来说显而易见的答案是“几次加入”。 But here's the situation: I have a table with a couple thousand entries, tableA. 但是情况是这样的:我有一个表,其中有数千个条目,即tableA。 I have another table with several thousand entries, tableB. 我有另一个包含数千个表的表tableB。 Each entry has a number the correlates with the id of a row in tableA. 每个条目都有一个与tableA中一行ID相关的数字。 There will be multiple rows in tableB that go with a single row in tableA. tableB中将有多行,而tableA中将有一行。 There will be a third table, tableC, with tens of thousands of entries. 将有第三个表tableC,其中包含成千上万的条目。 It will have multiple rows that correspond to a single row in tableB. 它将具有与tableB中的单个行相对应的多个行。

The query might look something like this: (the specifics don't really matter) 该查询可能看起来像这样:(具体细节并不重要)

SELECT tableA.*, tableB.*, tableC.* LEFT JOIN tableB ON tableB.idA = tableA.id LEFT JOIN tableC ON tableC.idB = tableB.id WHERE tableA.id = some_number;

This works just fine and dandy, but I get tons of duplicate rows because of the joining. 这很好并且很花哨,但是由于连接,我得到了大量重复行。 I want ONE result for tableA, a few results from tableB, and several from tableC. 我想要一个关于tableA的结果,一些来自tableB的结果,以及几个来自tableC的结果。

Unfortunately to do this I use many nested PHP arrays and for loops. 不幸的是,为此我使用了许多嵌套的PHP数组和for循环。 It gets to be messy, so I'm wondering if I should just use 3 separate queries to get rid of all the nested PHP loops, or if I should stick with the JOINS? 事情变得一团糟,所以我想知道是否应该只使用3个单独的查询来摆脱所有嵌套的PHP循环,还是应该坚持使用JOINS?

Additional info: "tableA": jobs id (primary key) name description date hours customer status 附加信息: “ tableA”:作业ID(主键)名称说明日期小时客户状态

"tableB": work id (primary key) job (correlates with id of row in jobs) summary description date “ tableB”:工作ID(主键)工作(与工作中行的ID相关)摘要描述日期

"tableC": times id (primary key) work (correlates with id of row in work) start end “ tableC”:时间id(主键)的工作时间(与工作中的行的id相关)开始结束

Desired output: I'm not really sure what kind of format would be best. 所需的输出:我不太确定哪种格式最好。 I want one job row, with a list of work that goes with it, and a list of times that goes with each work. 我想要一个作业行,其中包含与之相关的工作列表,以及与每个工作相关的时间列表。

Example of nested loops: $work is an array with two values. 嵌套循环的示例:$ work是具有两个值的数组。 The first is an array of works, the second is an array of times. 第一个是作品的数组,第二个是时间的数组。

foreach ($work[0] as $workk=>$workv) {
    echo("<div><h1>" . $workk . ": " . $workv["summary"] . "</h1><br/>Job: " . $workv["job"] . "</br>Date: " . $workv["date"] . "<br>Description:" . $workv["description"] . "<br/>Times:<br/>");
    foreach ($work[1] as $workid=>$times) {
        if ($workid == $workk) {
            foreach ($times as $time) {
                echo("Start: " . $time[0] . " | End: " . $time[1]);
            }
            break;
        }
    }
}

I get the "work" array using this code in a function (where $query->result() is a row from the long JOIN query) 我在函数中使用此代码获得“工作”数组(其中$ query-> result()是长JOIN查询中的一行)

foreach ($query->result() as $row) {
                if (!array_key_exists($row->id, $work)) {
                    $work[$row->id] = array("job"=>$row->job, "summary"=>$row->summary, "description"=>$row->description, "date"=>$row->date);
                }
                $times[$row->id][] = array($row->start, $row->end);
            }
            return array($work, $times);

You have two* ways of querying the DB: 您可以通过两种方式查询数据库:

  1. Get all the data together with two left joins. 获取所有数据以及两个左连接。
  2. Get the master data and load the details on demand. 获取主数据并按需加载详细信息。

* Do not load all the three tables separately and then try to link them in php * 不要分别加载所有三个表,然后尝试在php中链接它们

The first approach may take a bit longer to load at first, but as the data is loaded there will be no more DB calls. 首先,第一种方法可能需要更长的时间来加载,但是随着数据的加载,将不再有DB调用。 On the other hand, although it might be easy to show the data as a table (with repeating masters), often that is not the desired UI. 另一方面,尽管将数据显示为表格(带有重复的母版)可能很容易,但通常不是所需的UI。 Unless you are using a special UI component to automatically handle the master-details, you may have some difficulty converting the table to a more sensible form (in this case your loops). 除非您使用特殊的UI组件来自动处理主细节,否则您可能难以将表转换为更合理的形式(在这种情况下为循环)。

Your problem is in fact a UI design problem as how to show two levels of master-details. 您的问题实际上是一个UI设计问题,如如何显示两个主细节级别。 There are many UI patterns to show master-details. 有许多UI模式可显示主要细节。

For example, you can only load the first level (table A) in a drop down and query the DB and show the second level on drop down change. 例如,您只能在下拉菜单中加载第一级(表A)并查询数据库,并在下拉更改时显示第二级。 Or you could have a tree like structure that queries the db on collapse. 或者,您也可以使用类似树的结构在崩溃时查询数据库。

Take look at this link , to get some idea. 看一下这个链接 ,了解一些想法。 You could also try and search for master-detail UI components for php. 您也可以尝试搜索php的主从UI组件。

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

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