2 回答
TA贡献1934条经验 获得超2个赞
您可以使用GROUP_CONCAT
和GROUP BY
:
SELECT movie_name,movie_id, GROUP_CONCAT(genre SEPARATOR ' ') AS genre
FROM movies
JOIN movie_genre ON movies.movie_id = movie_genre.movie_id
JOIN genre ON movie_genre.genre_id = genre.genre_id
WHERE movie.movie_id = ?
GROUP BY movie_name,movie_id
TA贡献1780条经验 获得超1个赞
将需要为我无法测试的流派做一个循环,所以这只是将我想象的工作放在一起,但它会沿着这些方向进行,这里可能存在语法错误
$query = " SELECT movie_name,movie_id,genre FROM movies
JOIN movie_genre ON movies.movie_id = movie_genre.movie_id
JOIN genre ON movie_genre.genre_id = genre.genre_id
WHERE movie.movie_id = ?";
$stmt = mysqli_prepare($conn, $query);
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
if(mysqli_num_rows($result) > 0 ){
// if it's greater than zero, great, we have at least 1 movie.
// but if it's greater than 1, then we know we have more than 1 genre
while ($row = mysqli_fetch_assoc($result)) {
echo 'movie_id:'.$row["movie_id"].'movie name:'.$row["movie_name"];
break; // so it only runs through this loop once, instead of using limit
}//end while
if(mysqli_num_rows($result) > 1 ){
// more than 1 genre so loop and print out just genres
while ($row = mysqli_fetch_assoc($result)) {
echo $row["genre"];
}//end while
}//end if more than 1 genre
}//end if
- 2 回答
- 0 关注
- 93 浏览
添加回答
举报