0

具体的には、テーブルのキー/値タイプにある N 個のメタ値に基づいてワードプレスの投稿をクエリしたいと考えています。

機能するクエリの良い例が見つかりません。

平易な英語では、クエリは次のようになります。

city=Dallas、style=ranch、price between 100k から 200k、pool=true であるすべての投稿を選択します

比較する必要があるメタ値が多かれ少なかれある場合があります。

ワードプレス以外のユーザーの場合、メタ値は 1 つのテーブルにあり、各メタは posts テーブルの投稿 ID に関連付けられています。

4

1 に答える 1

3

ああ、EAVの喜び。つまり、複数のサブクエリまたはクロス集計を実行する必要があります。

Select ...
From Posts
Where post_id In    (
                    Select post_id
                    From Meta
                    Where Attribute = 'City'
                        And Value = 'Dallas'
                    )
    And post_id In  (
                    Select post_id
                    From Meta
                    Where Attribute = 'Style'
                        And Value = 'Ranch'
                    )
    And post_id In  (
                    Select post_id
                    From Meta
                    Where Attribute = 'Price'
                        And Cast(Value As int) Between 100000 And 200000
                    )
    And post_id In  (
                    Select post_id
                    From Meta
                    Where Attribute = 'Pool'
                        And Value = 'True'
                    )   

クロス集計を作成する別のフォームを次に示します。よりコンパクトですが、パフォーマンスが低下する場合があります。

Select ...
From Posts As P
    Join    (
            Select post_id
                , Min( Case When Attribute = 'City' Then Value End ) As City
                , Min( Case When Attribute = 'Style' Then Value End ) As Style
                , Min( Case When Attribute = 'Price' Then Cast(Value As int) End ) As Price
                , Min( Case When Attribute = 'Pool' Then Value End ) As Pool
            From Meta
            Where Attribute In('City','Style','Price','Pool')
            Group By post_id
            ) As Attributes
        On Attributes.post_id = P.post_id
Where Attributes.City = 'Dallas'
    And Attributes.Style = 'Ranch'
    And Attributes.Price Between 100000 And 200000
    And Attributes.Pool = 'True'
于 2011-03-05T15:41:02.750 に答える