简体   繁体   English

为什么我的查询返回不正确的结果?

[英]Why is my query returning incorrect results?

I recently asked a similar question to this about two weeks ago, but I have had no look in getting my problem working the way I need it to. 大约两周前我最近问了一个类似的问题,但是我没有看到我的问题以我需要的方式运行。

I have a query that is selecting from 2 tables; 我有一个从2个表中选择的查询; tickets and replies. 门票和回复。 I'm selecting the information from the ticket table, and I'm selecting the information from the replies table which has the ticket id stored in there. 我正在从故障单表中选择信息,并且我从回复表中选择了存储有票证ID的信息。 Which now comes to my problem. 这就是我的问题。

My query is only displaying tickets that have more than 0 replies, but I need it to display the ticket information even if it doesn't have any replies. 我的查询只显示回复超过0的票证,但我需要它才能显示票证信息,即使它没有任何回复。

I would like to know (if possible) if there is any way to fix my problem, and if there is a way to make it simpler than I currently have it. 我想知道(如果可能的话)是否有任何方法可以解决我的问题,如果有办法让它比我现在拥有它更简单。

It's a bit messy right now, but here is my is code to query and display the tickets and replies. 现在它有点乱,但这是我的代码,用于查询和显示故障单和回复。

 if(isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = trim($_GET['id']);
    $i = "";

    $ticket = $db->conn->query("
                                SELECT * FROM tickets
                                INNER JOIN replies ON tickets.id = '$id'") or die(mysqli_error($db->conn));

    while($rows = $ticket->fetch_assoc()) {
        $i++;
        if($_SESSION['ticket_username'] == $rows['client']) {
            if($i <= 1) {
                $status = $rows['status'];
                echo '
                    <h3>'.$rows['subject'].'</h3><hr>

                    <div class="panel panel-danger">
                    <div class="panel-heading">
                        <h3 class="panel-title"><small>Created by '.$rows['client'].', '.$timeAgo->inWords($rows['created_at']).'</small></h3>
                    </div>
                    <div class="panel-body">'.nl2br($rows['message']).'</div>
                    </div>
                ';
            }

            echo '
                <div class="panel panel-info">
                    <div class="panel-heading">
                        <h3 class="panel-title"><small>Reply from '.$rows['reply_username'].', '.$timeAgo->inWords($rows['reply_time']).'</small></h3>
                    </div>
                    <div class="panel-body">'.nl2br($rows['reply_message']).'</div>
                    </div>
                ';

        } else {
            header("Location: index");
        }
    }
} else {
    header("Location: index");
}

Change the query. 更改查询。

Use a LEFT JOIN instead of an INNER JOIN 使用LEFT JOIN而不是INNER JOIN

$ticket = $db->conn->query("SELECT * FROM tickets
LEFT JOIN replies ON tickets.id = '$id'") or die(mysqli_error($db->conn));

在此输入图像描述

Explanation: 说明:

  1. Inner Join returns only those values for both the tables get the match. 内部联接仅返回两个表的值获得匹配。 This is a kind of intersection of two circles thing. 这是两个圆圈的交集。
  2. Left Join returns all the rows from left table irrespective of whether it has any match on the right table. Left Join返回左表中的所有行,而不管它是否在右表中有任何匹配。

In your case, if you use inner join, it was returning only those tickets who have replies. 在您的情况下,如果您使用内部联接,它只返回那些有回复的票证。

Image Referred from here 图片来自这里

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

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