douqian8238 2013-08-19 19:05
浏览 78
已采纳

将两个不同的查询组合成一个合并的foreach

I'll try and explain this best I can. I have two queries pulling data from MySQL, one pulls data on park reviews, the other pulls data on ride reviews. Both queries are sorted by Review Date.

What I want on the web page is one list of reviews sorted by Review Date so park reviews and ride reviews will be mixed together in the one list.

Almost like two foreach loops running, one for each query but taking it in turns based on the date.

These are the two queries: For Rides

$query4= 'SELECT review_id, review, tpf_reviews_rides.user, DATE_FORMAT(date_ridden, "%d %M %Y") AS Date_Ridden, DATE_FORMAT(review_date, "%d %M %Y") AS Review_Date, tpf_rides.name AS ride_name, rating, tpf_parks.name  AS park_name, country
FROM tpf_reviews_rides  

INNER JOIN tpf_rides ON tpf_reviews_rides.ride_id = tpf_rides.ride_id
INNER JOIN tpf_ratings_rides ON tpf_reviews_rides.rating_link = tpf_ratings_rides.rating_id  
INNER JOIN tpf_parks ON tpf_reviews_rides.park_id = tpf_parks.park_id

ORDER BY review_date DESC, review_id DESC LIMIT '.$start_from.' , '.$limit.'';

For Parks:

$query5= 'SELECT review_id, review, tpf_reviews_parks.user, DATE_FORMAT(date_visited, "%d %M %Y") AS Date_Visited, DATE_FORMAT(review_date, "%d %M %Y") AS Review_Date, rating, tpf_parks.name, country
FROM tpf_reviews_parks  

INNER JOIN tpf_ratings_parks ON tpf_reviews_parks.rating_link = tpf_ratings_parks.rating_id  
INNER JOIN tpf_parks ON tpf_reviews_parks.park_id = tpf_parks.park_id

ORDER BY review_date DESC, review_id DESC LIMIT '.$start_from.' , '.$limit.'';

and this is how separately I display the results: For Rides:

foreach ($res4 AS $row4) { 
            printf('
            <h2 style="display:inline;">%s</h2> <h3 style="display:inline;">- %s, %s</h3>
            <h3>Rating - %sStars</h3>
            <h4>Submitted by %s on %s</h4>
            <p>%s</p>' . PHP_EOL, $row4['ride_name'], $row4['park_name'], $row4['country'], $row4['rating'], (htmlspecialchars($row4['user'], ENT_QUOTES, 'UTF-8')), $row4['Review_Date'], nl2br(htmlspecialchars($row4['review'], ENT_QUOTES, 'UTF-8')));

            if(!empty($row4['Date_Ridden'])){
            printf('<p>Date ridden: %s</p>' . PHP_EOL, $row4['Date_Ridden']);
            }

            printf('<a class="bloglink" href="#top">Back to Top</a>
            <hr>');

        }


    For PArks:

foreach ($res5 AS $row5) { 
    printf('
    <h2 style="display:inline;">%s</h2> <h3 style="display:inline;">- %s</h3>
    <h3>Rating - %sStars</h3>
    <h4>Submitted by %s on %s</h4>
    <p>%s</p>' . PHP_EOL, $row5['name'], $row5['country'], $row5['rating'], (htmlspecialchars($row5['user'], ENT_QUOTES, 'UTF-8')), $row5['Review_Date'], nl2br(htmlspecialchars($row5['review'], ENT_QUOTES, 'UTF-8')));

    if(!empty($row4['Date_Ridden'])){
    printf('<p>Date Visited: %s</p>' . PHP_EOL, $row4['Date_Visited']);
    }

    printf('<a class="bloglink" href="#top">Back to Top</a>
    <hr>');

}

To make it trickier this page is paginated. Looking at it, because the two queries are so similar if there is a way to combine at query level it would be best and help keep the paginate code in tact. Anyone have any suggestions on how I can achieve this?

Thanks

  • 写回答

1条回答 默认 最新

  • dongleiqiao4906 2013-08-19 19:36
    关注

    You can use UNION to merge together two select statements.

    Note that if your tables are so similar, it may be better to merge them (not in the query, but the actual tables). I do not see any fields that are not completely the same in your tpf_reviews_rides compared to tpf_reviews_parks. Add a column denoting the review type (ride or park) and you should be set. It will simplify your queries and your processing logic.

    SELECT
        type,
        review_id,
        review,
        user,
        Date_Event,
        Review_Date,
        ride_name,
        rating,
        park_name,
        country
    FROM (
        SELECT
            'ride' as type,
            review_id,
            review,
            tpf_reviews_rides.user,
            DATE_FORMAT(date_ridden, "%d %M %Y") AS Date_Event,
            DATE_FORMAT(review_date, "%d %M %Y") AS Review_Date,
            tpf_rides.name AS ride_name,
            rating,
            tpf_parks.name AS park_name,
            country
        FROM
            tpf_reviews_rides  
            INNER JOIN tpf_rides ON tpf_reviews_rides.ride_id = tpf_rides.ride_id
            INNER JOIN tpf_ratings_rides ON tpf_reviews_rides.rating_link = tpf_ratings_rides.rating_id  
            INNER JOIN tpf_parks ON tpf_reviews_rides.park_id = tpf_parks.park_id
    
        UNION
    
        SELECT
            'park' as type,
            review_id,
            review,
            tpf_reviews_parks.user,
            DATE_FORMAT(date_visited, "%d %M %Y") AS Date_Event,
            DATE_FORMAT(review_date, "%d %M %Y") AS Review_Date,
            NULL as ride_name,
            rating,
            tpf_parks.name AS park_name,
            country
        FROM
            tpf_reviews_parks  
            INNER JOIN tpf_ratings_parks ON tpf_reviews_parks.rating_link = tpf_ratings_parks.rating_id  
            INNER JOIN tpf_parks ON tpf_reviews_parks.park_id = tpf_parks.park_id
    ) AS reviews
    ORDER BY
        review_date DESC,
        review_id DESC
    LIMIT ' . $start_from . ', ' . $limit;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计