dongrang9300 2013-01-24 07:57
浏览 33
已采纳

小组结果双重结果

I have been working on an application that runs the following query:

SELECT m.*, GROUP_CONCAT(g.title) as genres
FROM movie m
INNER JOIN genre_movie gm ON gm.movie_id = m.id
INNER JOIN genre g ON gm.genre_id = g.id
WHERE m.imdb_id = '454876'

When I run the query in my query browser or straight via SSH it returns the following results (example:)

enter image description here

In my PHP code I do the following to loop over the genres:

<?php $get =  mysql_query($query); // $query is the above query ?>
<?php $movieInfo = mysql_fetch_assoc($get); ?>
<?php $genres = explode(",", $movieInfo['genres']); ?>
<?php foreach ($genres as $genre) { ?>
    <li class="clear"><a><span class="value"><?php echo $genre; ?></span></a></li>
<?php } ?>

While the expected out is expected to be:

Drama, Adventure

The output is:

Drama, Drama, Drama, Adventure, Adventure, Adventure

Any idea why this is happening as I can't figure it out?

PS: I know a work-around would be to just add array_unique() when I explode the comma separated list of the genres but I want to know the root of the problem.

Basically when I run the query manually the genres show up fine, when it runs in the applications, it shows double while the rest of the info still shows just fine.

  • 写回答

3条回答 默认 最新

  • dongqianwei6664 2013-01-24 08:15
    关注

    It seems like you have forgotten GROUP BY clause in SQL, due to which duplicate records are written.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大