【问题标题】:MySQL, need help defining multiple join query to return complete infoMySQL,需要帮助定义多个连接查询以返回完整信息
【发布时间】:2026-01-31 10:35:01
【问题描述】:

所以,我修改了从this thread 学到的查询,但是当我在标签和猫之间进行过滤时,结果并不理想。过滤类别 5 将仅返回类别列表信息,标签将为空,而标签则相反。

    SELECT posts.id,time,title,
        GROUP_CONCAT(IFNULL(cats.id, '') ORDER BY cats.id DESC SEPARATOR '~') as catIdList,
        GROUP_CONCAT(IFNULL(cats.name, '') ORDER BY cats.id DESC SEPARATOR '~') as catNameList,
        GROUP_CONCAT(IFNULL(cats.slug, '') ORDER BY cats.id DESC SEPARATOR '~') as catSlugList,
        GROUP_CONCAT(IFNULL(cats.value, '') ORDER BY cats.id DESC SEPARATOR '~') as catValueList,
        GROUP_CONCAT(IFNULL(tags.id, '') ORDER BY tags.id DESC SEPARATOR '~') as tagIdList,
        GROUP_CONCAT(IFNULL(tags.name, '') ORDER BY tags.id DESC SEPARATOR '~') as tagNameList,
        GROUP_CONCAT(IFNULL(tags.slug, '') ORDER BY tags.id DESC SEPARATOR '~') as tagSlugList,
        GROUP_CONCAT(IFNULL(tags.value, '') ORDER BY tags.id DESC SEPARATOR '~') as tagValueList
    FROM posts
    LEFT JOIN termRelations ON ( posts.id = termRelations.postId )
    LEFT JOIN cats ON ( termRelations.termId = cats.id AND termRelations.termTypeId = 1 )
    LEFT JOIN tags ON ( termRelations.termId = tags.id AND termRelations.termTypeId = 0 )
    WHERE ( ( IFNULL(tags.id, '') = '4' ) )
    GROUP BY posts.id ORDER BY time DESC

IFNULL() 可以解决不存在的条目。上面的查询将返回:

    (
        [id] => 15
        [time] => 0
        [title] => post 15
        [catIdList] => 
        [catNameList] => 
        [catSlugList] => 
        [catValueList] => 
        [tagIdList] => 4
        [tagNameList] => tagname
        [tagSlugList] => tagname
        [tagValueList] => 
    )

    (
        [id] => 16
        [time] => 0
        [title] => post 16
        [catIdList] => 
        [catNameList] => 
        [catSlugList] => 
        [catValueList] => 
        [tagIdList] => 4
        [tagNameList] => tagname
        [tagSlugList] => tagname
        [tagValueList] => 
    )

如果没有WHERE ( ( IFNULL(tags.id, '') = '4' ) ),结果将是(当然还有所有其他帖子,因为它没有被过滤到这个标签中):

    (
        [id] => 15
        [time] => 0
        [title] => post 15
        [catIdList] => 
        [catNameList] => 
        [catSlugList] => 
        [catValueList] => 
        [tagIdList] => 4
        [tagNameList] => tagname
        [tagSlugList] => tagname
        [tagValueList] => 
    )

    (
        [id] => 16
        [time] => 0
        [title] => post 16
        [catIdList] => 5~~
        [catNameList] => Movies~~
        [catSlugList] => movies~~
        [catValueList] => ~~
        [tagIdList] => 4~1~
        [tagNameList] => tagname~sand~
        [tagSlugList] => tagname~sand~
        [tagValueList] => ~~
    )

这当然是我想要的 - 所有相关信息!

这些表格是:

  1. termRelations 包含 post ID 和 term ID,termTypeId 区分 cat 和 tags 表。
  2. cats 包含术语 ID 和类别信息(名称、slug、parentId 等)
  3. tags 包含术语 ID 和标签信息(名称、slug 等)
  4. posts 包含帖子信息(标题、时间、正文等)

termRelations 的目的是将标签和类别绑定到帖子。此查询的目的是返回过滤结果(我希望用户能够查看具有特定标签和特定类别的帖子。)同时仍保留完整信息。

是否可以通过将catstags 表合并到terms 中来解决这个问题?

我希望我知道怎么做,但是在这一点上,我几乎碰到了心理障碍。所以,一点帮助:)?谢谢!!

【问题讨论】:

    标签: php mysql join table-relationships


    【解决方案1】:

    使用子查询将WHERE 更改为EXIST

    SELECT posts.id,time,title,
        GROUP_CONCAT(IFNULL(cats.id, '') ORDER BY cats.id DESC SEPARATOR '~')
          AS catIdList,
        GROUP_CONCAT(IFNULL(cats.name, '') ORDER BY cats.id DESC SEPARATOR '~') 
          AS catNameList,
        GROUP_CONCAT(IFNULL(cats.slug, '') ORDER BY cats.id DESC SEPARATOR '~') 
          AS catSlugList,
        GROUP_CONCAT(IFNULL(cats.value, '') ORDER BY cats.id DESC SEPARATOR '~') 
          AS catValueList,
        GROUP_CONCAT(IFNULL(tags.id, '') ORDER BY tags.id DESC SEPARATOR '~') 
          AS tagIdList,
        GROUP_CONCAT(IFNULL(tags.name, '') ORDER BY tags.id DESC SEPARATOR '~') 
          AS tagNameList,
        GROUP_CONCAT(IFNULL(tags.slug, '') ORDER BY tags.id DESC SEPARATOR '~') 
          AS tagSlugList,
        GROUP_CONCAT(IFNULL(tags.value, '') ORDER BY tags.id DESC SEPARATOR '~') 
          AS tagValueList
    FROM posts
    LEFT JOIN termRelations ON ( posts.id = termRelations.postId )
    LEFT JOIN cats 
      ON ( termRelations.termId = cats.id AND termRelations.termTypeId = 1 )
    LEFT JOIN tags 
      ON ( termRelations.termId = tags.id AND termRelations.termTypeId = 0 )
    WHERE EXISTS
          ( SELECT *
            FROM termRelations 
            WHERE termRelations.termId = '4'
              AND termRelations.termTypeId = 0
              AND posts.id = termRelations.postId
          )
    GROUP BY posts.id 
    ORDER BY time DESC
    

    【讨论】:

    • 我将posts.id = termRelations.postId AND 添加到子查询(以及您的答案)的 WHERE 中,然后它就起作用了!谢谢你。我喜欢堆栈溢出。
    • 是的,你是对的,我不知何故错过了这个条件。子查询旨在成为一个相关的(与您的添加完全相同)。