简体   繁体   English

mysql查询不正确的输出

[英]mysql query incorrect output

I've a mysql table of simple data like: 我有一个简单数据的mysql表,例如:

替代文字

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: 它输出:

替代文字

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: 它输出:

替代文字

please, help me... 请帮我...


sql of the table & data: 表和数据的SQL:

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');

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". 您的现金返还列是VARCHAR,它是一种文本类型,因此min(),max()等均按字母排序(正常)进行操作,因此“ b”>“ aaaa”,当然也包括“ 5”>“ 100” 。

Use the correct types. 使用正确的类型。

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', 您正在对VARCHAR列进行排序-现金返还,字符串'5'大于字符串'40',换句话说,如果您将'5'和'40'排在字典中,则'5'在'40'之后,因此结果MAX-'5'和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%' 在另一种情况下-如果您在字典中排列“ 46%”和“ 44%”,则“ 46%”比“ 44%”晚,因此MAX“ 46%”和MIN“ 44%”

Your cashback field is incorrect. 您的cashback字段不正确。 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. 对不起我的英语不好。

cashback seem to be a varchar column and for such '40' < '5' is true. 现金返还似乎是varchar列,因此'40'<'5'是正确的。 I'm not sure if the following works with mysql: 我不确定以下是否适用于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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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