区别不消除空结果

时间:2013-09-23 13:41:01

标签: mysql distinct group-concat

我的应用程序使用带有关系表的表,我用来连接带有类别的演员,演员和标签GROUP_CONCAT和我的SQL中的DISTINCT。

我面临的问题是我也会为演员输出拇指,但有时候演员还没有缩略图。在这种情况下,我在group_concat行中生成的值与actor字段不一致导致3个actor是唯一的但是如果我有2个没有图像的actor(null)我丢失了它们的锚点。

e.g。

SELECT
  videos.id AS id, 
  videos.video_title AS video_title,
  videos.video_views AS video_views, 
  videos.video_likes AS video_likes, 
  videos.video_dislikes AS video_dislikes, 
  videos.video_duration AS video_duration,
  GROUP_CONCAT(DISTINCT a.actor_name SEPARATOR ';') AS actor_names,
  GROUP_CONCAT(DISTINCT t.tag_name SEPARATOR ';') AS tag_names,
  GROUP_CONCAT(DISTINCT c.category_name SEPARATOR ';') AS category_names,
  GROUP_CONCAT(DISTINCT a.actor_thumb SEPARATOR ';') AS actor_thumbs

FROM videos 

LEFT OUTER JOIN video_actors  AS va ON va.video_id = videos.id
LEFT OUTER JOIN actor AS a ON a.actor_id = va.actor_id

LEFT OUTER JOIN video_tags AS vt ON vt.video_id = videos.id
LEFT OUTER JOIN tags AS t ON t.tag_id = vt.tag_id

LEFT OUTER JOIN video_categories AS vc ON vc.video_id = videos.id
LEFT OUTER JOIN categories AS c ON c.category_id = vc.category_id

WHERE videos.id = '23'

Actor thumb field结果:

more fields.. |0;http://site.com/actor/59.jpg

但它应该是(3个演员,2个没有缩略图):

more fields.. |0;0;http://site.com/actor/59.jpg

将值与演员姓名保持一致。

希望这有点清楚。

提前致谢! 尼克

1 个答案:

答案 0 :(得分:1)

最好立即获得演员姓名和拇指......

GROUP_CONCAT(DISTINCT CONCAT(a.actor_name,'^',a.actor_thumb) SEPARATOR ';') AS actor_names