0

I've a mysql table of simple data like:

alt text

now when i execute this simple query:

SELECT MAX(cashback) as MAX, MIN(cashback) as MIN FROM pearlcashback_retailers_category WHERE retailer_id='32' AND cashback NOT LIKE '%\%'

it outputs:

alt text

and for this query:

SELECT MAX(cashback) as MAX, MIN(cashback) as MIN FROM pearlcashback_retailers_category WHERE retailer_id='32' AND cashback LIKE '%\%'

it outputs:

alt text

please, help me...


sql of the table & data:

CREATE TABLE IF NOT EXISTS `pearlcashback_retailers_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `retailer_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `cashback` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;


INSERT INTO `pearlcashback_retailers_category` (`id`, `retailer_id`, `category_id`, `cashback`) VALUES
(1, 32, 17, '46%'),
(2, 32, 14, '40'),
(11, 4, 15, '27%'),
(9, 32, 15, '5'),
(8, 32, 7, '44%'),
(14, 3, 13, '1');
3
  • You have two different queries and they are working fine....what is the question? what help do you need? Commented Dec 19, 2010 at 11:09
  • see the output of both queries.... isn't they wrong??? Commented Dec 19, 2010 at 11:12
  • No, its not wrong...check my answer. Commented Dec 19, 2010 at 11:39

4 Answers 4

2

Your cashback column is VARCHAR which is a text type, therefore min(), max() etc all operate on alphabetical sorting (which is normal), therefore "b" > "aaaa", and of course "5" > "100".

Use the correct types.

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

Comments

1

You are sorting a VARCHAR column - cashback, string '5' is greater than string '40', in other words if you arrange '5' and '40' as in a dictionary, '5' comes after '40', hence ther result MAX - '5' and MIN '40',

In the other case - if you arrange '46%' and '44%' as in a dictionary, '46%' comes later than '44%', hence MAX '46%' and MIN '44%'

Comments

0

Your cashback field is incorrect. Data should be in one format. If you store in this field only percentages symbol % is not required. It will be good for your query and your architecture. Sorry for my english.

2 Comments

Is that an answer or a comment?
yes... i know but as i am first electing only the numeric fields not with % fields.. so, why the output is wrong??
0

cashback seem to be a varchar column and for such '40' < '5' is true. I'm not sure if the following works with mysql:

SELECT MAX(cashback) as MAX, MIN(cashback) as MIN 
FROM pearlcashback_retailers_category 
WHERE retailer_id='32' AND cashback NOT LIKE '%\%'
ORDER BY cast(cashback as integer)

Comments

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.