简体   繁体   中英

mysql not returning proper results for this query

Hi I have a column km ( varchar ) in a table called products and and we save km values like how much a car has been driven. So a km could be any number that is saved. Now I want to query this column like I want results where km from 100 to 1000.

so the query looks like this

SELECT * 
  FROM products1 
 WHERE categories_category_id = 1 
   AND subsubCategoryId = 1026 
   AND subcat = 174 
   AND categoryAdvertiseTypeId = 0 
   AND km >= 1000 
   AND km <= 5000 
 LIMIT 50

but what is happening is I get km not from this range. dont know why. Please tell me what could be the issue.

Cast your kilometer km column to a number and then do the comparison.

SELECT *
FROM products1
WHERE
    categories_category_id = '1'  AND
    subsubCategoryId = '1026'     AND
    subcat = '174'                AND
    categoryAdvertiseTypeId = '0' AND
    CAST(km AS UNSIGNED) BETWEEN 1000 AND 5000
LIMIT 50

On the cosmetic side of things, I removed all those unnecessary backticks, which are ugly and make the query hard to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM