简体   繁体   中英

Inner join function duplicates content

My query duplicates the entries I get. I use two Inner join. The strange fact is that I made this code 6 months ago and, as far as I remember, it worked. Now, I come back to my project and it does not work anymore (it duplicates)...

The Query :

$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."";

The résult :

It duplicates each entry 3 times.

同一条目各出现3次

The problem

If I delete the second inner join, there is no more duplicate.

$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."";

Does anyone could say why?

[EDIT] Here are the Tables structures

sys_pre_values

档案1/3

档案2/3

档案3/3

bx_blogs_posts

you may use GROUP BY

try this

   $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."";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM