SQL WordPress 自定义查询内连接
我的 SQL 语句遇到一些困难。我正在 WordPress 上进行查询,以根据多个帖子元字段显示帖子。当我仅使用一个元字段或多个元字段进行查询和过滤时,它可以工作,但是多个元字段上的 AND 会失败。
SELECT wposts . *
FROM wp_posts wposts
INNER JOIN (
SELECT post_id
FROM wp_postmeta wpostmeta
WHERE (
(wpostmeta.meta_key = 'ulnooweg_business_industry'
AND wpostmeta.meta_value = 'Legal Services')
AND (
wpostmeta.meta_key = 'ulnooweg_business_province'
AND wpostmeta.meta_value = 'New Brunswick')
)
GROUP BY post_id
)
AS t ON t.post_id = wposts.ID
WHERE wposts.post_status = 'publish'
AND wposts.post_type = 'business'
ORDER BY wposts.post_title ASC
LIMIT 0 , 30
I'm having some difficulty with my SQL statement. I'm doing a query on WordPress to display posts based on multiple post meta fields. When I do the query and filter with only one meta field, or a OR on multiple it works, however AND on multiple fails.
SELECT wposts . *
FROM wp_posts wposts
INNER JOIN (
SELECT post_id
FROM wp_postmeta wpostmeta
WHERE (
(wpostmeta.meta_key = 'ulnooweg_business_industry'
AND wpostmeta.meta_value = 'Legal Services')
AND (
wpostmeta.meta_key = 'ulnooweg_business_province'
AND wpostmeta.meta_value = 'New Brunswick')
)
GROUP BY post_id
)
AS t ON t.post_id = wposts.ID
WHERE wposts.post_status = 'publish'
AND wposts.post_type = 'business'
ORDER BY wposts.post_title ASC
LIMIT 0 , 30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在子查询中,看起来它正在查找同时包含
wpostmeta.meta_key = 'ulnooweg_business_industry'
和wpostmeta.meta_key = 'ulnooweg_business_province'
的记录 - 换句话说,wpostmeta.meta_key
需要同时等于两个字符串才能满足此条件。此外,它还在寻找wpostmeta.meta_value = 'Legal Services'
和wpostmeta.meta_value = 'New Brunswick'
。我的猜测是,这就是您在子查询的
WHERE
子句中想要的 - 将AND
之一更改为OR
:In the subquery, it looks like it's looking for records where both
wpostmeta.meta_key = 'ulnooweg_business_industry'
andwpostmeta.meta_key = 'ulnooweg_business_province'
-- in other words,wpostmeta.meta_key
needs to be equal to two strings simultaneously to satisfy this condition. Also, it's looking forwpostmeta.meta_value = 'Legal Services'
andwpostmeta.meta_value = 'New Brunswick'
.My guess is that this is what you want in the
WHERE
clause of the subquery -- change one of theAND
s to anOR
:问题出在内部 select 的 where 子句中;我猜 wpostmeta 返回多行。之前关于字符串不能是两个值的评论是正确的。如果第一种方法不能
一开始我认为,
第二种方法应该有效。只有当每种类型的条目在 wpostmeta 中只有一条记录时,这种方法才有效。如果
postmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services' 可以出现两次,那么上面的方法不起作用。
第二种方法
The problem is in the where clause of your inner select; I'm guessing wpostmeta returns MULTIPLE rows. A previous comment that a string can't be two values is correct. The 2nd approach should work if the 1st doesn't
at first I thought
This will work ONLY if there is only one record in the wpostmeta for each type of entry. If
postmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services' can occur twice, then the above does't work.
2nd approach
您的查询正在测试meta_key(和meta_value)是否是同一行中的2个不同值,这是不可能的。但我明白你想要做什么..
尝试加入 wp_postmeta 表两次,但每次都使用 ON 子句,该子句排除除满足 meta_key 条件之外的所有行:
注意:我在这里加入了 wp_postmeta 表 3 次,以帮助证明满足条件,但您可以删除 GROUP_CONCAT 行(当然还有前一行上的逗号)和第一个 JOIN 到 wp_postmeta ,查询将同样工作。
Your query is testing if meta_key (and meta_value) is 2 different values in the same row, which is impossible. But I see what you are trying to do..
Try joining the wp_postmeta table twice except each with an ON clause that excludes all rows except those that satisfy the meta_key condition:
Note: I joined the wp_postmeta table 3 times here to help prove that the conditions are satisfied, but you can remove the GROUP_CONCAT line (and the comma on the previous line of course) and the first JOIN to wp_postmeta and the query will work the same.