如何将四个多表连接成一个结果?

时间:2017-03-18 20:11:09

标签: php mysql join

我有一个问题,我真的不知道自己在做什么。我想要创建的是这样的。

我有4张桌子。

movies | movie_categories
series | show_categories

现在我想创建一个将连接所有这四个表并在代码中返回结果的查询,这是我到目前为止所尝试的,并且它无法正常工作

<?php
    if(isset($_GET['category_id']) && !empty($_GET['category_id']))
    {
        $kategorija = $_GET['category_id'];

        /*$query = $connection->prepare('SELECT * FROM movies LEFT JOIN movie_categories ON movie_categories.movie_hash = movies.hash WHERE movie_categories.category LIKE ? ORDER BY movies.id DESC');*/
        $query = $connection->prepare("

                SELECT
                movie_categories.movie_hash,
                movie_categories.movie_category,
                show_categories.serie_hash,
                show_categories.serie_category
                FROM movie_categories
                INNER JOIN show_categories ON movie_categories.movie_hash != show_categories.serie_hash
                LEFT JOIN movies on movie_categories.movie_hash = movies.hash
                LEFT JOIN series on show_categories.serie_hash = series.hash
                WHERE movie_categories.movie_category LIKE ? 
                OR WHERE show_categories.serie_category LIKE ? 
        ");



        $query->bindValue(1, "%$kategorija%", PDO::PARAM_STR);
        $query->bindValue(2, "%$kategorija%", PDO::PARAM_STR);
        $query->execute();      

        if($query->rowCount() > 0)
        {
            $check = $query->fetchAll(PDO::FETCH_ASSOC);
            foreach($check as $row)
            {
                echo '
                    <div class="row" id="content">
                        <div class="col-md-2 col-sm-4 col-xs-6 animacija animated fadeInDown">
                            <div class="film">
                                <a href="watch.php?movie=',$row['hash'],'" title="',$row['name'],'">
                                <img alt="',$row['name'],'" width="165" height="250" class="img-responsive" src="',$row['thumb_location'],'">
                                    <span class="play animated flip">
                                    <i class="fa fa-play-circle fa-4x"></i>
                                    </span>
                                    </a>
                                    <div class="opis">
                                    <a href="watch.php?movie=',$row['hash'],'" title="',$row['name'],' "><h2>',$row['name'],' </h2></a>
                                    <div class="tekst">
                                    </div>
                                    <p>
                                    <span>
                                    <a href="godina.php?movie=',$row['year'],'" title="',$row['year'],'">',$row['year'],'</a>
                                    </span>
                                </p>
                            </div>
                        </div>
                    </div>
                ';          
            }
        }
        else
        {
            echo '<h3>U ovoj kategoriji nema filmova.</h3>';
        }
    }
    else
    {
        header("location: index.php");
        exit();
    }

?>

0 个答案:

没有答案
相关问题