9

これは、 WordPressに実装されているMySQLに関する非常に具体的な質問です。

特定の「タグ」を持ち、特定の「カテゴリ」(両方とも複数)に属する投稿を表示(選択)するプラグインを開発しようとしています

カテゴリとタグの保存方法が原因で不可能だと言われました。

  1. wp_posts投稿のリストが含まれ、各投稿には「ID」があります
  2. wp_terms用語のリスト(カテゴリとタグの両方)が含まれています。各用語にはTERM_IDがあります
  3. wp_term_taxonomyTERM_IDを含む用語のリストがあり、それらのそれぞれ(カテゴリまたはタグのいずれか)の分類法の定義があります
  4. wp_term_relationships用語と投稿の間に関連付けがあります

テーブルを結合して、カテゴリ「Category1」にも属するタグ「Nuclear」「Deals」を持つすべての投稿を取得するにはどうすればよいですか?

4

6 に答える 6

5

私はあなたを誤解しました。私はあなたが核か取引が欲しいと思った。以下はあなたに核と取引だけを与えるべきです。

select p.*
from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id

and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id

and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id

and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')
于 2008-08-27T17:57:29.053 に答える
2

これを試して:

select p.*
from wp_posts p, 
wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2

where p.id = tr.object_id
and t.term_id = tt.term_id
and tr.term_taxonomy_id = tt.term_taxonomy_id

and p.id = tr2.object_id
and t2.term_id = tt2.term_id
and tr2.term_taxonomy_id = tt2.term_taxonomy_id

and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))

基本的に、関連する子テーブルの2つのコピー(terms、term_taxonomy、およびterm_relationship)を使用しています。1つのコピーは「Category1」制限を適用し、もう1つのコピーは「Nuclear」または「Deals」制限を適用します。

ところで、これは核取引に関するすべての投稿でどのようなプロジェクトですか?あなたは私たちを政府のリストに載せようとしていますか?;)

于 2008-08-26T15:29:20.120 に答える
2

なんて大まかなDB構造。

とにかく、私はこのようなことをしたいと思います (結合よりも EXISTS を好むことに注意してください。ただし、必要に応じて結合として書き直すことができます。ほとんどのクエリ アナライザーはとにかくそれらを同じクエリ プランにまとめます)。それを機能させるには、何らかの方法で追加のジャグリングを行う必要がある場合があります...

SELECT *
  FROM wp_posts p
 WHERE EXISTS( SELECT *
                 FROM wp_term_relationship tr
                WHERE tr.object_id = p.id
                  AND EXISTS( SELECT *
                                FROM wp_term_taxonomy tt
                               WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                 AND tt.taxonomy         = 'category'
                                 AND EXISTS( SELECT *
                                               FROM wp_terms t
                                              WHERE t.term_id = tt.term_id
                                                AND t.name    = "Category1" 
                                           )
                            )
                  AND EXISTS( SELECT *
                                FROM wp_term_taxonomy tt
                               WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                 AND tt.taxonomy         = 'post_tag'
                                 AND EXISTS( SELECT *
                                               FROM wp_terms t
                                              WHERE t.term_id = tt.term_id
                                                AND t.name    = "Nuclear" 
                                           )
                                 AND EXISTS( SELECT *
                                               FROM wp_terms t
                                              WHERE t.term_id = tt.term_id
                                                AND t.name    = "Deals" 
                                           )
                            )
            )
于 2008-08-26T14:41:27.160 に答える
1

そこで、WordPressデータベースで両方のオプションを試しました。投稿で「Perl」と「Programming」のタグが付いたカテゴリ「Tech」を探しました。

最初のselectステートメントに欠落しているコンマを追加すると、Ericは機能しました。3レコードを返しました。問題は、「post_tag」を探しているセクションが実際にはORオプションとして機能していることです。私の投稿の1つには、両方ではなく1つのタグしかありませんでした。また、SELECTDISTINCTを作成するとよいでしょう。

Mattのバージョンを試しましたが、空のセットが返され続けました。私はそれと「ジャグリング」しようとするかもしれません。

于 2008-08-26T23:14:28.893 に答える
0

本当にとても素晴らしい答え..私を大いに助けてくれました..

素晴らしいbcoz。複雑なクエリを作成するための基本的なアプローチを教えてくれました!

私のような準備ができているユーザーのために、1つの小さな修正:)

「wp_term_relationship」は「存在しないエラー」を返します..正しいテーブル名であるため、wp_term_relationshipsを使用します。

ありがとうエリック

于 2009-02-05T15:26:06.137 に答える
0

ありがとう@Ericそれは動作します!今後の参考のために、いくつかのコードを修正します。

  • wp_term_relationship tr2 の後の最初の select ステートメントでコンマが欠落している
  • 同じ select ステートメントで、以下を変更する必要があります。
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 

tr2

する必要があります

wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 

tr3
于 2008-08-28T11:22:40.257 に答える