开发者

mySQL query from two different tables with mutual conditional

开发者 https://www.devze.com 2023-03-31 16:49 出处:网络
Please help me to structure mysql query I have 2 tables, #_udjacomment AND #_content currently I have query:

Please help me to structure mysql query

I have 2 tables, #_udjacomment AND #_content

currently I have query:

$query  = "SELECT udja.id";

      if( $include_author == 1 ) $query .= ", udja.full_name";
      if( $include_date == 1 ) $query .= ", udja.time_added";
      if( $include_comment == 1 ) $query .= ", if(CHAR_LENGTH(udja.content) > ".$content_number_of_characters.", SUBSTR(udja.content, 1, ".$content_number_of_characters."), udja.content) AS content";
      if( $include_link_to_comment == 1 ){ 
        $query .= ", CASE WHEN LOCATE('com_content:', udja.comment_url) > 0 
        THEN CONCAT(SUBSTRING_INDEX(udja.comment_url,':',-1),'-', com_content.alias, '.html')
        ELSE udja.comment_url END AS comment_url";
        }

      $query .= " FROM #__udjacomments AS udja, #__content AS com_content WHERE udja.is_published = 1 AND com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1) AND com_content.checked_out = 0 ORDER by udja.id DESC limit ".$number_of_comments;

But I am not getting the proper results. If I stop trying to access from the table #__content AS com_content, then I get the results for #__udjacomment AS udja correct

So, I guess I am asking how can indicate and include the constrain that I want the field com_content.alias WHERE com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1)

In some cases, udja.comment_url will have this format com_content:22, com_content:19

and in other instances, udja.comment_url will have a string like word-word-another-word

this is why I have the more extensive statement inside the conditional if($include_link_to_comment == 1)

UPDATE: THE FINAL QUERY LOOKED LIKE THIS (I IMPLEMENTED WHAT RESPONDER SUGGESTED AND CHANGED THE CASE STATEMENT AND THE WHERE STATEMENT)

 $query  = "SELECT udja.id";

      if( $include_author == 1 ) $query .= ", udja.full_name";
      if( $include_date == 1 ) $query .= ", udja.time_added";
      if( $include_comment == 1 ) $query .= ", if(CHAR_LENGTH(udja.content) > ".$content_number_of_characters.", SUBSTR(udja.content, 1, ".$content_number_of_characters."), udja.content) AS content";
      if( $include_link_to_comment == 1 ){ 
        $query .= ", CASE 
            WHEN LOCATE('com_content:', udja.comment_url)<>0
            THEN CONCAT(SUBSTRING_INDEX(udja.comment_url,':',-1),'-', com_content.alias, '.html')
            ELSE udja.comment_url 
            END AS comment_url";

        }
//          THEN CONCAT(SUBSTRING_INDEX(udja.comment_url,':',-1),'-', com_content.alias, '.html')
      $query .= " FROM #__udjacomments AS udja
                    LEFT JOIN #__content AS com_content 
                        ON com_content.id开发者_JAVA百科 = SUBSTRING_INDEX(udja.comment_url,':',-1) 
                    WHERE udja.is_published = 1 ORDER by udja.id DESC limit ".$number_of_comments;


You need to use an outer join:

...
FROM #__udjacomments AS udja
LEFT JOIN #__content AS com_content 
    on com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1)
WHERE ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号