我会尽力解释这个。我有两个从MySQL中提取数据的查询,一个从公园评论中提取数据,另一个从车辆评论中提取数据。两个查询都按Review Date
排序。
我在网页上想要的是一个按Review Date
排序的评论列表,因此公园评论和评论将在一个列表中混合在一起。
几乎像两个foreach循环运行,每个查询一个,但根据日期轮流进行。
这是两个查询: 对于游乐设施
$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.'';
对于公园:
$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.'';
这就是我显示结果的方式: 对于游乐设施:
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>');
}
为了使它变得更加棘手,这个页面是分页的。看一下,因为如果有一种方法可以在查询级别进行组合,那么两个查询是如此相似,这将是最好的并且有助于保持分页代码。有人对我如何实现这一点有任何建议吗?
由于
答案 0 :(得分:0)
您可以使用UNION
将两个选择语句合并在一起。
请注意,如果您的表非常相似,那么合并它们可能会更好(不是在查询中,而是在实际的表中)。我tpf_reviews_rides
与tpf_reviews_parks
相比,我看不到任何字段不完全相同。添加表示评论类型(骑行或公园)的列,您应该进行设置。它将简化您的查询和处理逻辑。
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;