在wordpress中(自定义查询还可以)我如何根据邮政编码和最高票数从每个分类中只获得1个帖子 所以我有2个自定义字段 1)ZIP CODE(字段名称zip_code) 2)投票(字段名称doctor_ratings)
我无法在线托管我的数据库所以它here(只有3个表)
并且我喜欢在单个查询中检索(用于速度优化)所有具有最大投票率的所有词汇分类法的约束(投票如null,1,1.5,2,2.5(最多5)因此这里的投票是小数点是我试过的
除了从这里回答,我唯一的方法是循环所有医生
SELECT SQL_CALC_FOUND_ROWS abcde_posts.ID
FROM abcde_posts
INNER JOIN abcde_term_relationships ON ( abcde_posts.ID = abcde_term_relationships.object_id )
INNER JOIN abcde_postmeta ON ( abcde_posts.ID = abcde_postmeta.post_id )
INNER JOIN abcde_postmeta AS mt1 ON ( abcde_posts.ID = mt1.post_id )
WHERE 1 =1
AND (
abcde_term_relationships.term_taxonomy_id
IN ( 11, 13, 14, 15, 17, 16, 12, 3, 18 )
)
AND abcde_posts.post_type = 'doctors'
AND (
(
abcde_posts.post_status = 'publish'
)
)
GROUP BY abcde_posts.ID
LIMIT 0 , 30
答案 0 :(得分:0)
您的问题并未解释您对postmeta
的使用情况,因此我将猜测您是如何使用它的。
对于初学者来说,伪查询代码中你需要的就是这个。首先获得每个邮政编码的最高评级。
SELECT zip, MAX(rating) AS rating /*pseudocode*/
FROM doctors
GROUP BY zip
然后,您将该查询用作子查询,以便通过zip获取医生的身份和订单。您正在查找具有您在找到的拉链中找到的评级的医生。 (如果有两个文档并列为最高评级,那么你可以得到它们。)
SELECT d.id, d.name, d.rating, d.zip /*pseudocode*/
FROM doctors AS d
JOIN (
SELECT zip, MAX(rating) AS rating
FROM doctors
GROUP BY zip
) AS q ON d.zip = q.zip AND d.rating = q.rating
ORDER BY d.zip, d.id
问题在于:使用postmeta
有点痛苦。它需要一种特殊的查询模式来检索特定列中的元数据。您为所需的每个元项执行此操作。它使复杂的查询模式看起来比它们更复杂。
SELECT whatever, whatever,
zip.meta_value AS zip
FROM wp_posts
LEFT JOIN wp_postmeta AS zip ON wp_posts.ID = zip.post_id AND zip.meta_key='zip_code'
LEFT JOIN
从postmeta中获取一个meta_key值并将其转换为查询中的一列。
所以,你需要做两次,一次是拉链,一次是评级。使用您的表名称即可获得此名称。您应该检查结果,以确保在继续之前获得正确的信息。
SELECT
abcde_posts.ID,
abcde_posts.post_title AS NAME,
zip.meta_value AS zip,
rating.meta_value AS rating
FROM abcde_posts
LEFT JOIN abcde_postmeta AS zip
ON abcde_posts.ID = zip.post_id AND zip.meta_key = 'zip_code'
LEFT JOIN abcde_postmeta AS rating
ON abcde_posts.ID = rating.post_id AND rating.meta_key = 'doctor_ratings'
WHERE 1 = 1
AND abcde_posts.post_type = 'doctors'
AND abcde_posts.post_status = 'publish'
上面的伪代码是你的doctors
表。所以你需要在查询中使用它,喘气,两次,以获得你想要的东西。
SELECT
d.id,
d.name,
d.rating,
d.zip
FROM (SELECT
abcde_posts.ID,
abcde_posts.post_title AS NAME,
zip.meta_value AS zip,
rating.meta_value AS rating
FROM abcde_posts
LEFT JOIN abcde_postmeta AS zip
ON abcde_posts.ID = zip.post_id AND zip.meta_key = 'zip_code'
LEFT JOIN abcde_postmeta AS rating
ON abcde_posts.ID = rating.post_id AND rating.meta_key = 'doctor_ratings'
WHERE 1 = 1
AND abcde_posts.post_type = 'doctors'
AND abcde_posts.post_status = 'publish') AS d
JOIN (SELECT zip,
MAX(rating) AS rating
FROM (SELECT
abcde_posts.ID,
abcde_posts.post_title AS NAME,
zip.meta_value AS zip,
rating.meta_value AS rating
FROM abcde_posts
LEFT JOIN abcde_postmeta AS zip
ON abcde_posts.ID = zip.post_id AND zip.meta_key = 'zip_code'
LEFT JOIN abcde_postmeta AS rating
ON abcde_posts.ID = rating.post_id AND rating.meta_key = 'doctor_ratings'
WHERE 1 = 1
AND abcde_posts.post_type = 'doctors'
AND abcde_posts.post_status = 'publish') AS doctors
GROUP BY zip) AS q ON d.zip = q.zip AND d.rating = q.rating
ORDER BY d.zip, d.id
如果您的托管服务和WordPress安装允许,您可以创建一个VIEW
来定义doctors
表。这将使您的查询更具可读性:您可以使用我直接显示的伪代码。
我希望你的眼睛不会太糟糕。如果您记住SQL意味着结构化查询语言,那么这个大查询实际上相当简单,而结构化意味着您可以在查询中嵌套查询。它冗长而简单。这就是我开始使用伪代码的原因。