简体   繁体   中英

JOIN query not returning all results

In my admin panel I have access to View/Edit/Delete privileges, and I need to have access to view reviews that users have posted, along with the album name and date. At the moment, I am using a LEFT JOIN, but that only brings up the date and the review, not the album name.

if(isset($_GET['id'])){

    $get_id = $_GET['id'];

    require_once 'database_conn.php';

    $sql = "SELECT cdreview.reviewText, 
                   cdreview.reviewDate, 
                   cd.CDTitle 
            FROM cdreview 
            LEFT JOIN cd 
              ON cdreview.CDID = cd.CDID 
            WHERE cdreview.userID = '$get_id' 
            ORDER BY reviewDate ASC";

    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

    $row = mysqli_fetch_array($result);

        $date = $row['reviewDate'];
        $album = $row['CDTitle'];
        $review = $row ['reviewText'];
    }

?>

Any help is appreciated.

In your db model CDReview should have FK to CD because you can only review existing CD , so you should use INNER JOIN

The fact you are doing LEFT JOIN make me think you can delete CD . So my guess is you delete the CD for that review.

So check your db model, and if possible show us your CREATE TABLE in the question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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