Order query results by two key/value pairs, where the fields for the key/value pairs are the same

 ̄綄美尐妖づ 提交于 2019-12-12 03:08:30

问题


I have a query to select posts (wp_posts table) where either 1 key/value pair or another is present on the a second table (wp_postmeta). Esentially, this works, but I then need to order the results by the first key/pair value (department_head), and then the second (staff_surname).

The problem is, becuause of the way the results are grabbed by the query, there is no department_head key to use to order the results by. I'm guessing that this is because the query is finding the staff_surname key first?

Again guessing, but if my above guess is correct then it is because, even though I'm joining the table twice, MYSQL is just grabbing the first instance from the wp_postmeta table when the staff_surname condition is met. This is what I need to find away around, so that I have the correct data to be able to order the results.

Full query -

SELECT SQL_CALC_FOUND_ROWS wp_posts.*
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
WHERE 1=1
AND (
    wp_term_relationships.term_taxonomy_id IN (34)
)
AND wp_posts.post_type = 'people'
AND (
    wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private'
)
AND (
    (
        wp_postmeta.meta_key = 'department_head'
        AND CAST(wp_postmeta.meta_value AS CHAR) = 'private-client'
    )
    OR (
        mt1.meta_key = 'staff_surname'
        AND CAST(mt1.meta_value AS CHAR) != ''
    )
)
GROUP BY wp_posts.ID
ORDER BY mt1.meta_value ASC
LIMIT 0, 10

Example of query results -

ID  name   meta_id post_id meta_key    meta_value    meta_id  post_id  meta_key       meta_value

1   Test1  10      1       random_key  random_value  11       1        staff_surname  Smith
2   Test1  20      2       random_key  random_value  21       2        staff_surname  Jones
3   Test1  30      3       random_key  random_value  31       3        staff_surname  Harris

Example of what I require, if Jones was the department head -

ID  name   meta_id post_id meta_key         meta_value      meta_id  post_id  meta_key       meta_value

1   Test1  10      1                                        11       1        staff_surname  Smith
2   Test1  22      2       department_head  private-client  21       2        staff_surname  Jones
3   Test1  30      3                                        31       3        staff_surname  Harris

The 'department_head' key does not exist in the vast majority of cases, that is why the key/value are blank on the other resutes.

Is there a way to achieve the ordering results that I require?

Note: The reason that I have posted this here as opposed to the Wordpress site is because this is a custom query, not WP generated, so it will likely be out of the scope of most WP experts.


回答1:


I'm not sure but I will try to approach this with some LEFT JOINS and moving the conditions to the JOINS to make the query more efficient.

Something similar to this may work...

SELECT p.*
FROM wp_posts p
INNER JOIN wp_term_relationships r ON 
    (p.ID=r.object_id AND r.term_taxonomy_id=34)
LEFT JOIN wp_postmeta m1 ON 
    (p.ID=m1.post_id AND m1.meta_key='department_head' AND CAST(m1.meta_value AS CHAR)='private-client')
LEFT JOIN wp_postmeta m2 ON 
    (p.ID=m2.post_id AND m2.meta_key = 'staff_surname' AND CAST(m2.meta_value AS CHAR) != '')
WHERE 
    p.post_type = 'people' 
  AND 
    (p.post_status = 'publish' OR p.post_status = 'private')
GROUP BY p.ID
ORDER BY m1.meta_value DESC, m2.meta_value DESC
LIMIT 0, 10


来源:https://stackoverflow.com/questions/9992248/order-query-results-by-two-key-value-pairs-where-the-fields-for-the-key-value-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!