1

I am working on a custom plugin in wordpress. I have a weird issue with one sql.

SQL:

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_term_taxonomy 
ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
WHERE 1 =1
AND wp_term_taxonomy.taxonomy = 'category'
AND wp_term_taxonomy.term_id IN ('23')
AND (
wp_posts.post_author =1
)
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'
)
GROUP BY wp_posts.ID
ORDER BY `wp_posts`.`as_stats_rating` DESC
LIMIT 0 , 30 

Its returning the correct data but its not sorting results according to as_stats_rating. I am stumped. Does anyone know what I am doing wrong?

Edit 1 : Update

Here is the structure of wp_posts: alt text


Sample result:

ID  as_stats_rating
1221   8
1222   10

All fields in sample results are :
ID post_author post_date post_date_gmt post_content post_title post_excerpt post_status comment_status ping_status post_password post_name to_ping pinged post_modified post_modified_gmt post_content_filtered post_parent guid menu_order post_type post_mime_type comment_count as_stats_numviews as_stats_numvotes as_stats_votestotal as_stats_rating


By the way, its not only about 'order by wp_posts.as_stats_rating', 'order by wp_posts.as_stats_numviews' have the same issue. (just to clear up, if you are wondering about as_stats_rating having varchar type)

8
  • i would suspect a conflict with the limit statement. as a check, take the limit off and see if the order by is correct. Commented Dec 11, 2010 at 14:38
  • @Randy : No. Just checked by removing limit clause, the issue persist. Commented Dec 11, 2010 at 14:45
  • It already has a group by, what should be the group by? Commented Dec 11, 2010 at 14:53
  • Is wp_post.ID an auto_increment? Commented Dec 11, 2010 at 15:09
  • What does a sample result set look like? Commented Dec 11, 2010 at 16:08

1 Answer 1

1

Any chance that the as_stats_rating field is a string (char/text) datatype and not a numeric (float/int/decimal) datatype?

Sign up to request clarification or add additional context in comments.

1 Comment

Hell. I am so sorry about this questions. As it turns out changing datatype to int solved the issue. Coming to think of it, it was sorting 8 > 10 as texts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.