内连接功能复制内容

时间:2014-02-15 08:34:22

标签: mysql sql inner-join

我的查询重复了我得到的条目。我用两个内连接。奇怪的事实是,我在6个月前制作了这段代码,据我记忆,它有效。现在,我回到我的项目,它不再起作用了(它重复)......

查询:

$sql_principale_news = "SELECT sys_pre_values.LKey,bx_blogs_posts.PostUri,bx_blogs_posts.PostCaption,bx_blogs_posts.PostText,bx_blogs_posts.PostText,bx_blogs_posts.Categories,
      bx_blogs_posts.PostDate,bx_blogs_posts.Views,bx_blogs_posts.CommentsCount,bx_blogs_posts.allowView,Profiles.Avatar,Profiles.ID,Profiles.FirstName,Profiles.LastName,Profiles.ProfileType
      FROM bx_blogs_posts
      INNER JOIN Profiles ON bx_blogs_posts.OwnerID=Profiles.ID AND Profiles.Status='Active'
      INNER JOIN sys_pre_values ON bx_blogs_posts.NewsType=sys_pre_values.Value AND sys_pre_values.Value!=sys_pre_values.LKey
      WHERE bx_blogs_posts.OpenNews=1 
        AND bx_blogs_posts.PostStatus='approval'
        AND bx_blogs_posts.Categories IN ('".$innov24_config."')
        AND (bx_blogs_posts.allowView=3 
          OR bx_blogs_posts.allowView=4 
          OR (bx_blogs_posts.allowView=5 AND bx_blogs_posts.OwnerID IN ('".$bigfriendlist."')) 
          OR bx_blogs_posts.allowView IN ('".$privacy_OthersToMe."') 
          OR bx_blogs_posts.allowView IN ('".$privacy_MeToOthers."'))
      ORDER BY bx_blogs_posts.PostDate DESC LIMIT ".$_GET['debut'].",".$nb_affichage_par_page."";

结果:

它会复制每个条目3次。

The same entry appears 3 times each

问题

如果删除第二个内部联接,则不会再有重复。

$sql_principale_news = "SELECT sys_pre_values.LKey,bx_blogs_posts.PostUri,bx_blogs_posts.PostCaption,bx_blogs_posts.PostText,bx_blogs_posts.PostText,bx_blogs_posts.Categories,
      bx_blogs_posts.PostDate,bx_blogs_posts.Views,bx_blogs_posts.CommentsCount,bx_blogs_posts.allowView,Profiles.Avatar,Profiles.ID,Profiles.FirstName,Profiles.LastName,Profiles.ProfileType
      FROM bx_blogs_posts
      INNER JOIN Profiles ON bx_blogs_posts.OwnerID=Profiles.ID AND Profiles.Status='Active'
      // DELETED CODE // INNER JOIN sys_pre_values ON bx_blogs_posts.NewsType=sys_pre_values.Value AND sys_pre_values.Value!=sys_pre_values.LKey// END DELETED CODE //
      WHERE bx_blogs_posts.OpenNews=1 
        AND bx_blogs_posts.PostStatus='approval'
        AND bx_blogs_posts.Categories IN ('".$innov24_config."')
        AND (bx_blogs_posts.allowView=3 
          OR bx_blogs_posts.allowView=4 
          OR (bx_blogs_posts.allowView=5 AND bx_blogs_posts.OwnerID IN ('".$bigfriendlist."')) 
          OR bx_blogs_posts.allowView IN ('".$privacy_OthersToMe."') 
          OR bx_blogs_posts.allowView IN ('".$privacy_MeToOthers."'))
      ORDER BY bx_blogs_posts.PostDate DESC LIMIT ".$_GET['debut'].",".$nb_affichage_par_page."";

有人能说出原因吗?

[编辑]以下是表格结构

sys_pre_values

Profiles 1/3

Profiles 2/3

Profiles 3/3

bx_blogs_posts

1 个答案:

答案 0 :(得分:0)

您可以使用GROUP BY

试试这个

   $sql_principale_news = "SELECT sys_pre_values.LKey,bx_blogs_posts.PostUri,bx_blogs_posts.PostCaption,bx_blogs_posts.PostText,bx_blogs_posts.PostText,bx_blogs_posts.Categories,
  bx_blogs_posts.PostDate,bx_blogs_posts.Views,bx_blogs_posts.CommentsCount,bx_blogs_posts.allowView,Profiles.Avatar,Profiles.ID,Profiles.FirstName,Profiles.LastName,Profiles.ProfileType
  FROM bx_blogs_posts
  INNER JOIN Profiles ON bx_blogs_posts.OwnerID=Profiles.ID AND Profiles.Status='Active'
   INNER JOIN sys_pre_values ON bx_blogs_posts.NewsType=sys_pre_values.Value AND sys_pre_values.Value!=sys_pre_values.LKey// END DELETED CODE //
  WHERE bx_blogs_posts.OpenNews=1 
    AND bx_blogs_posts.PostStatus='approval'
    AND bx_blogs_posts.Categories IN ('".$innov24_config."')
    AND (bx_blogs_posts.allowView=3 
      OR bx_blogs_posts.allowView=4 
      OR (bx_blogs_posts.allowView=5 AND bx_blogs_posts.OwnerID IN ('".$bigfriendlist."')) 
      OR bx_blogs_posts.allowView IN ('".$privacy_OthersToMe."') 
      OR bx_blogs_posts.allowView IN ('".$privacy_MeToOthers."'))
 GROUP BY sys_pre_values.LKey
  ORDER BY bx_blogs_posts.PostDate DESC LIMIT ".$_GET['debut'].",".$nb_affichage_par_page."";