Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORDER BY an equal value in MySQL

Tags:

mysql

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=$wpdb->posts.ID
JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id=$wpdb->posts.ID
WHERE 
$wpdb->posts.post_status = 'publish' 
AND $wpdb->posts.post_type = 'post'
AND $wpdb->term_relationships.term_taxonomy_id='$vendor_category_to_use'
AND $wpdb->postmeta.meta_key='zip'

I have a postmeta.meta_key called featured. Then the value of if the post is featured is in another column called postmeta.meta_value. If the meta_value = "yes" where the meta_key equals "featured" then I want to display this one first and then display the rest of the posts after that. How can I go about doing that?

EDIT: Here is the data setup:


postid       meta_key      meta_value
123          featured      yes
324          featured      no
182          featured      yes
873          featured      yes

So in this example I'd like my posts to be displayed in this order:


postid       meta_key      meta_value
123          featured      yes
182          featured      yes
873          featured      yes
324          featured      no

I hope that helps!

Here is the SQL that gets generated:

SELECT * 
FROM wp_posts 
JOIN wp_term_relationships ON wp_term_relationships.object_id=wp_posts.ID 
JOIN wp_postmeta ON wp_postmeta.post_id=wp_posts.ID 
WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' AND wp_term_relationships.term_taxonomy_id='5' AND wp_postmeta.meta_key='zip' AND (wp_postmeta.meta_value = '46320' OR wp_postmeta.meta_value = '46321' OR wp_postmeta.meta_value = '46322' OR wp_postmeta.meta_value = '46323' OR wp_postmeta.meta_value = '46324' OR wp_postmeta.meta_value = '46327' OR wp_postmeta.meta_value = '46394' OR wp_postmeta.meta_value = '46402' OR wp_postmeta.meta_value = '46404' ) 
ORDER BY (wp_postmeta.meta_key='featured' AND wp_postmeta.meta_value='yes') DESC, wp_posts.post_date DESC

Thanks!

like image 271
user1048676 Avatar asked Feb 23 '12 00:02

user1048676


People also ask

How does MySQL order rows with the same value?

The order that rows are returned in is guaranteed ONLY by ORDER BY clause (or in MySQL, an ORDER BY implicitly specified in the GROUP BY clause.) Apart from that, there is NO GUARANTEE of the order rows will be returned in. Apart from that, MySQL is free to return the rows in any sequence.

How do I sort a column by value in SQL?

To sort by a column: Type: SELECT columns FROM table ORDER BY sort_column [ASC | DESC]; columns is one or more comma-separated column names, sort_column is the name of the column on which to sort the result, and table is the name of the table that contains columns and sort_column.

How do I sort a value in MySQL?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Can we use two ORDER BY in same query?

After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name). You can modify the sorting order (ascending or descending) separately for each column.


2 Answers

SELECT ...
FROM   ...
ORDER BY (meta_key='featured' AND meta_value='yes') DESC, postid ASC;

If (meta_key='featured' AND meta_value='yes') for a row, that row will have a 1/TRUE. Otherwise, it will have a 0/FALSE. Hence, sorting descending puts the rows that have TRUE first.

like image 54
mathematical.coffee Avatar answered Nov 07 '22 16:11

mathematical.coffee


It is straightforward like this:

...
ORDER BY featured = 'yes'

featured = 'yes' is evaluates as boolean true or false, for items that featured = "yes" is true will come first then items that has featured = "yes" is false i.e featured = "no" come later.

Update:

You can use CASE statement in the order by clause to sort your data on a sepecific field, something like:

ORDER BY 
        CASE WHEN postmeta.meta_key = 'featured' 
             THEN meta_value = 'yes' DESC  
        END

OR:

ORDER BY ( postmeta.meta_key = 'featured' AND meta_value = 'yes') DESC

You might need to add another order by criteria in the ELSE part of the CASE statement to order the items that have meta_key = 'featured is false to order them by.

If the field metadata_key is always = "featured" then you can get rid of the case statement and use just ORDER BY meta_value = 'yes' DESC

like image 21
Mahmoud Gamal Avatar answered Nov 07 '22 15:11

Mahmoud Gamal