Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Single/Double digit sorting errors in mysql query

Answered my own questions. Thanks to plenty of googling, I learned that I needed to use the cast() function to consider the values as numbers instead of as text strings.

This is the code I ended up with:

$most_viewed = (array) $bbdb->get_results("SELECT topic_id, meta_value FROM $bbdb->topicmeta WHERE meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC");

The cast($column as $type) function forces the query to treat the meta_value values as unsigned integers. Nifty, yes?

Skip to toolbar