2

そのため、このスレッドから学習したクエリを変更しましたが、タグと猫の間でフィルタリングすると、結果は望ましくありません。カテゴリ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投稿IDと用語IDが含まれ、termTypeIdは猫とタグのテーブルを区別します。
  2. cats用語IDとカテゴリ情報(名前、スラッグ、parentIdなど)が含まれています
  3. tags用語IDとタグ情報(名前、スラッグなど)が含まれています
  4. posts投稿情報(タイトル、時間、本文など)が含まれています

termRelationsの目的は、タグとカテゴリを投稿にバインドすることです。このクエリの目的は、完全な情報を保持しながら、フィルタリングされた結果を返すことです(ユーザーが特定のタグと特定のカテゴリの投稿を表示できるようにする必要があります)。

catstagsテーブルを組み合わせることでこれを解決できる可能性はありtermsますか?

方法を知っていればよかったのですが、この時点で私はこれについて精神的な壁にぶつかっています。だから、少し助けます:)?ありがとう!!

4

1 に答える 1

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
于 2012-01-04T00:36:13.030 に答える