mySQL 从两个具有相互条件的不同表进行查询

发布于 2024-12-02 08:01:35 字数 2560 浏览 0 评论 0原文

请帮助我构建 mysql 查询

我有 2 个表,#_udjacomment 和 #_content

目前我有查询:

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

但我没有得到正确的结果。如果我停止尝试从表 #__content AS com_content 访问,那么我会得到 #__udjacomment AS udja 正确的结果

所以,我想我是在问如何指示并包含我想要的字段 com_content.alias 的约束WHERE com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1)

在某些情况下,udja.comment_url 将具有此格式com_content:22、com_content:19

在其他情况下,udja.comment_url 将有一个像 word-word-another-word 这样的字符串,

这就是为什么我在条件 if($include_link_to_comment == 1) 中有更广泛的语句

更新:THE最终查询如下所示(我实施了答复者的建议并更改了案例陈述和WHERE 语句)

 $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 = SUBSTRING_INDEX(udja.comment_url,':',-1) 
                    WHERE udja.is_published = 1 ORDER by udja.id DESC limit ".$number_of_comments;

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 = SUBSTRING_INDEX(udja.comment_url,':',-1) 
                    WHERE udja.is_published = 1 ORDER by udja.id DESC limit ".$number_of_comments;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-12-09 08:01:35

您需要使用外连接:

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

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 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文