Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my query returning incorrect results?

Tags:

php

mysql

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; 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. 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.

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");
}
like image 353
Jon Avatar asked Jul 10 '15 12:07

Jon


People also ask

How do I check if a SQL query is correct?

Check - The check is a way for you to check if you have written a legal SQL query. Arrow - This is the execute command button. This will send the query to the server and the server will write back the result to you. Square - This is the stop execution command.

Why is SQL query returning duplicates?

If you do not include DISTINCT in a SELECT clause, you might find duplicate rows in your result, because SQL returns the JOB column's value for each row that satisfies the search condition. Null values are treated as duplicate rows for DISTINCT.


1 Answers

Change the query.

Use a LEFT JOIN instead of an INNER JOIN

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

enter image description here

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.

In your case, if you use inner join, it was returning only those tickets who have replies.

Image Referred from here

like image 57
Pupil Avatar answered Sep 28 '22 02:09

Pupil