简体   繁体   English

如何在三个表上运行两个查询并在一个表中输出列的结果

[英]How can i run two queries on three tables and output the result of a column in one table

I tried coming up with a better title, and I wish there was a chat that I could join to better discuss what i mean. 我尝试提出一个更好的标题,但我希望能与我聊天以加入讨论,以更好地讨论我的意思。 Anyway, I have a while loop running that pulls data from two tables and displays the results correctly. 无论如何,我有一个while循环正在运行,该循环从两个表中提取数据并正确显示结果。 I have a third table that I want to display the name of a result based on what id comes out in a loop... See what i mean, it doesn't make sense. 我有第三张表,我想根据循环中出现的ID显示结果的名称...明白我的意思,这没有任何意义。

Here is the queries and loop: 这是查询和循环:

                try {
                $stmt = $Conn->prepare("SELECT * FROM wn_trailer "
                        . "INNER JOIN wn_trailer_history ON wn_trailer.id = wn_trailer_history.trailer_id "
                        . "ORDER BY trailer_number ASC");
                $stmt->execute();

                $stmt2 = $Conn->prepare("SELECT status_name FROM wn_trailer_status WHERE status_id = :status");
                $stmt2->execute([":status" => $row[1]]);

                while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {

                    $data = "<tr>\r\n<td><a href='view-trailer.php?id=$row[0]'>" . $row[1] . "</a></td>\r\n"
                            . "<td>" . $row[1] . "</td>\r\n"
                            . "<td>" . $row[16] . "</td>\r\n"
                            . "<td><a href='https://www.google.com/maps/search/?api=1&query=$row[3],$row[4]' target='_blank'>View Map</a></td>\r\n"
                            . "</tr>\r\n";
                    print $data;
                }
            } catch (PDOException $e) {
                print $e->getMessage();
            }

$stmt2 is the second query that is hitting the third table. $ stmt2是命中第三个表的第二个查询。 As the loop runs i want it to compare a result from the first query, and if it finds the id to return the value in the column. 在循环运行时,我希望它比较第一个查询的结果,以及是否找到ID以返回列中的值。 The query is good, i checked it in the mysql log, but it is not returning a value. 查询是好的,我在mysql日志中检查了它,但是没有返回值。

Another option i was thinking about doing is writing a function that runs through the wn_trailer_status and creates a select case, and using that to compare the results that why i'm not banging against the database a bunch of times. 我正在考虑做的另一种选择是编写一个在wn_trailer_status中运行并创建选择用例的函数,然后使用该函数比较结果,以说明为什么我没有多次冲击数据库。

tldr; tldr; My question is how can i compare the results from the first query against the second query and display the result of a column, or should i use a select case function to handle the work load? 我的问题是如何将第一个查询的结果与第二个查询的结果进行比较,并显示列的结果,还是应该使用select case函数处理工作量?

assuming the value status is retun by the first query form table wn_trailer you could resolve both the query using adding an inner join 假设第一个查询表单表wn_trailer恢复了值状态,则可以通过添加内部联接来解析这两个查询

 $stmt = $Conn->prepare("SELECT * 
             FROM wn_trailer  
             INNER JOIN wn_trailer_history ON wn_trailer.id = wn_trailer_history.trailer_id 
             INNER JOIN wn_trailer_status  ON wn_trailer_status.status_id = wn_trailer.status
             ORDER BY trailer_number ASC");

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

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