SQL WordPress 自定义查询内连接

发布于 2024-12-12 01:44:53 字数 633 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漆黑的白昼 2024-12-19 01:44:54

在子查询中,看起来它正在查找同时包含 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

....
WHERE (
  (wpostmeta.meta_key = 'ulnooweg_business_industry'
    AND wpostmeta.meta_value = 'Legal Services')
  OR ( -- changed to an OR
  wpostmeta.meta_key = 'ulnooweg_business_province'
    AND wpostmeta.meta_value = 'New Brunswick')
)
....

In the subquery, it looks like it's looking for records where both wpostmeta.meta_key = 'ulnooweg_business_industry' and wpostmeta.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 for wpostmeta.meta_value = 'Legal Services' and wpostmeta.meta_value = 'New Brunswick'.

My guess is that this is what you want in the WHERE clause of the subquery -- change one of the ANDs to an OR:

....
WHERE (
  (wpostmeta.meta_key = 'ulnooweg_business_industry'
    AND wpostmeta.meta_value = 'Legal Services')
  OR ( -- changed to an OR
  wpostmeta.meta_key = 'ulnooweg_business_province'
    AND wpostmeta.meta_value = 'New Brunswick')
)
....
最单纯的乌龟 2024-12-19 01:44:54

问题出在内部 select 的 where 子句中;我猜 wpostmeta 返回多行。之前关于字符串不能是两个值的评论是正确的。如果第一种方法不能

一开始我认为

WHERE
  ((wpostmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services') OR 
   (wpostmeta.meta_key = 'ulnooweg_business_province' AND wpostmeta.meta_value = 'New Brunswick'))
Group by Post_ID
HAVING count(post_ID) = 2

第二种方法应该有效。只有当每种类型的条目在 wpostmeta 中只有一条记录时,这种方法才有效。如果
postmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services' 可以出现两次,那么上面的方法不起作用。

第二种方法

Select wposts.*
FROM WP_Posts wposts
INNER JOIN  (
Select POST_ID from WP_POSTMeta where meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services'
INTERSECT
SELECT POST_ID FROM WP_POST_META WHERE meta_key = 'ulnooweg_business_province' AND wpostmeta.meta_value = 'New Brunswick'
) 
AS T on T.Post_ID = wposts.ID

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

WHERE
  ((wpostmeta.meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services') OR 
   (wpostmeta.meta_key = 'ulnooweg_business_province' AND wpostmeta.meta_value = 'New Brunswick'))
Group by Post_ID
HAVING count(post_ID) = 2

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

Select wposts.*
FROM WP_Posts wposts
INNER JOIN  (
Select POST_ID from WP_POSTMeta where meta_key = 'ulnooweg_business_industry' AND wpostmeta.meta_value = 'Legal Services'
INTERSECT
SELECT POST_ID FROM WP_POST_META WHERE meta_key = 'ulnooweg_business_province' AND wpostmeta.meta_value = 'New Brunswick'
) 
AS T on T.Post_ID = wposts.ID
吖咩 2024-12-19 01:44:53

您的查询正在测试meta_key(和meta_value)是否是同一行中的2个不同值,这是不可能的。但我明白你想要做什么..

尝试加入 wp_postmeta 表两次,但每次都使用 ON 子句,该子句排除除满足 meta_key 条件之外的所有行:

SELECT 
    p.*, 
    GROUP_CONCAT(CONCAT(pm.meta_key,':',pm.meta_value) SEPARATOR ',') AS meta_values
FROM 
    wp_posts p
    JOIN wp_postmeta pm ON pm.post_id = p.ID
    JOIN wp_postmeta pm_bi ON (pm_bi.post_id = p.ID AND pm_bi.meta_key = 'ulnooweg_business_industry')
    JOIN wp_postmeta pm_bp ON (pm_bp.post_id = p.ID AND pm_bp.meta_key = 'ulnooweg_business_province')
WHERE 
    pm_bi.meta_value = 'Legal Services'
    AND pm_bp.meta_value = 'New Brunswick'
    AND p.post_type = 'business'
    AND p.post_status = 'publish'
GROUP BY p.ID
ORDER BY p.post_title ASC

注意:我在这里加入了 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:

SELECT 
    p.*, 
    GROUP_CONCAT(CONCAT(pm.meta_key,':',pm.meta_value) SEPARATOR ',') AS meta_values
FROM 
    wp_posts p
    JOIN wp_postmeta pm ON pm.post_id = p.ID
    JOIN wp_postmeta pm_bi ON (pm_bi.post_id = p.ID AND pm_bi.meta_key = 'ulnooweg_business_industry')
    JOIN wp_postmeta pm_bp ON (pm_bp.post_id = p.ID AND pm_bp.meta_key = 'ulnooweg_business_province')
WHERE 
    pm_bi.meta_value = 'Legal Services'
    AND pm_bp.meta_value = 'New Brunswick'
    AND p.post_type = 'business'
    AND p.post_status = 'publish'
GROUP BY p.ID
ORDER BY p.post_title ASC

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文