ポストとポストメタの 2 つのテーブルがあります。
posts
ID title category post_status post_type
1 ABC cat-1 Publish Store
2 DEF cat-2 Publish Store
3 GHI cat-3 Publish Store
4 JKL cat-2 Publish Store
5 XYZ cat-5 Draft Store
6 MNO cat-9 Publish Article
と
postmeta
meta_id post_id meta_key meta_value
109 1 city 1
110 1 featured h
111 2 city 1,2
112 2 featured both
113 3 city 2,3
114 3 featured both
115 4 city 1
116 4 featured n
117 5 city 1,4
118 5 featured h
119 6 city 1
120 6 featured h
次の条件を持つ投稿のリストを取得するクエリを実行しようとしています。
- 都市に対する値の AND が 1 である
- Featured の値が h または両方の AND である
- 投稿ステータスが公開かつ
- 投稿タイプがストアの場合
- タイトル順に並べる
私がしようとしているクエリは
SELECT DISTINCT posts.ID , posts.*, postmeta.*
FROM posts, postmeta
WHERE posts.ID = postmeta.post_id
AND (postmeta.meta_value = 'h' OR postmeta.meta_value = 'both')
AND (postmeta.meta_key = 'post_city_id' AND (postmeta.meta_value LIKE '%,1,%' OR postmeta.meta_value LIKE '%1,%' OR postmeta.meta_value LIKE '%,1%' OR postmeta.meta_value LIKE '%1%'))
AND posts.post_status = 'Publish'
AND posts.post_type = 'Store'
ORDER BY (SELECT postmeta.meta_value from postmeta where (posts.ID = postmeta.post_id) and postmeta.meta_key LIKE '%home_featured_type%') asc, posts.post_title LIMIT 0,6
正しい戻り値は ID 1 と 2、つまり abc と def です。しかし、私は空の結果を得ています。どこが崩れているのか見当がつきません。これはどのように修正できますか?