ORDER BY an equal value in MySQL

后端 未结 3 1737
情深已故
情深已故 2020-12-30 15:52

All, I\'ve got the following SQL query as of now:

SELECT * FROM $wpdb->posts
JOIN $wpdb->term_relationships ON $wpdb->term_relationships.object_id=$         


        
3条回答
  •  独厮守ぢ
    2020-12-30 16:16

    Also, you can use MySQL function named

    FIELD(HAYSTACK, value[, ...values]).
    

    This function returns index of haystack in values (FROM 1 to n, 0 - if not found).

    Example

    ORDER BY FIELD(wp_postmeta.meta_key, 'featured', wp_postmeta.meta_key), 
    FIELD(wp_postmeta.meta_value, 'yes', wp_postmeta.meta_value), wp_posts.post_date DESC
    

    But, in your case, use the mathematical.coffee example. In my example you need to repeat the field name in the end of list, because if you don't - it will return 0 (zero) and elements than not in list will be first in result statement.

提交回复
热议问题