简体   繁体   中英

join 3 tables with where clause

This query performs three JOIN operations with 3 tables. But is not ok i see..i'm trying to output all the rows in echo, but i have bad luck.

Mysql table columns:

tours
------
titlu_slider | desc_slider | poza_slider | poza_articol | pret

tours_review 
----------
name | time_added | review_text

tours_overview
------------
descriere | titlu_box1 | desc_box1 | titlu_box2 | desc_box2 | titlu_box3 | desc_box3 | titlu_box4 | desc_box4

Php code:

<?php 
 $db = mysqli_connect("localhost", "root", "fidodido", "antonytravel");
 $q = mysqli_query($db,"SELECT * FROM tours  INNER JOIN tours_review INNER JOIN tours_overview  WHERE id = ".$_GET['id']."");

while ($row = mysqli_fetch_assoc($q)) {
$titlu_slider=$row['titlu_slider'];
$desc_slider=$row['desc_slider'];
$poza_slider=$row['poza_slider'];
$poza_articol=$row['poza_articol'];
$pret=$row['pret'];
## Review table
$name_review=$row['name'];
$time_added=$row['time_added'];
$review_text=$row['review_text'];
## Overview table
$descriere=$row['descriere'];
$titlu_box1=$row['titlu_box1'];
$desc_box1=$row['desc_box1'];
$titlu_box2=$row['titlu_box2'];
$desc_box2=$row['desc_box2'];
$titlu_box3=$row['titlu_box3'];
$desc_box3=$row['desc_box3'];
$titlu_box4=$row['titlu_box4'];
$desc_box4=$row['desc_box4'];

    echo '<section class="parallax_window_in" data-parallax="scroll" data-image-src="'.$poza_slider.'" data-natural-width="1400" data-natural-height="470">
        <div id="sub_content_in">
            <div id="animate_intro">
                <h1>'.$titlu_slider.'</h1>
                <p>"'.$desc_slider.'"</p>
            </div>

        </div>';

Some help needed..thanx.

You need to specify how how the tables relate to each other which might look something like the on conditions shown below (which are just guesses)

SELECT * 
FROM tours t
INNER JOIN tours_review trev 
INNER JOIN tours_overview tovr 
WHERE t.id = $whatever

You then face the issue of what type of join because if you have a tour with no reviews then you probably still want to list it. For that type of relationship you need an "outer join".

SELECT * 
FROM tours t
 tours_review trev ON t.id = trev.tour_id
INNER JOIN tours_overview tovr ON = t.id = tovr.tour_id
WHERE t.id = $whatever

If every every tour has an "overview" then that can remain an "inner join"

EDIT: Please note that you need to prefix EVERY column reference with a table name or table alias (I have used table aliases to make the query shorter). If you don't do this your query may fail, eg if every table has a column id and you just ask for where id = 123 the query will not know which table to use and the query would error.

INNER join shows the records if there are matching record. Use OUTER join to show all records if it does not exists on other tables.

You are missing a few things in your query. Specifically related to the fields that link the tables. To do these joins the best practice is to name each table and then use that name to in an ON statement to JOIN the tables

So

SELECT * FROM tours 
INNER JOIN tours_review
INNER JOIN tours_overview  
WHERE id = ".$_GET['id'].""

Should be:

SELECT * FROM tours AS t 
INNER JOIN tours_review AS r ON r.somefield = t.somefield  
INNER JOIN tours_overview AS o ON o.somefield = t.somefield  
WHERE id = ".$_GET['id'].""

MySQL can't join tables if it doesn't know what is connecting them.

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