简体   繁体   English

MySQL查询像数字大于x

[英]Mysql query like number greater than x

I have a field for comments used to store the title of the item sold on the site as well as the bid number (bid_id). 我有一个评论字段,用于存储网站上出售的商品的标题以及出价编号(bid_id)。 Unfortunately, the bid_id is not stored on its own in that table. 不幸的是,bid_id并未单独存储在该表中。

I want to query items that have a number (the bid_id) greater than 4,000 for example. 例如,我要查询数字(bid_id)大于4,000的项目。

So, what I have is: 所以,我有:

select * from mysql_table_name where comment like '< 4000'

I know this won't work, but I need something similar that works. 我知道这行不通,但是我需要类似的东西。

Thanks a lot! 非常感谢!

There is nothing ready that works like that. 没有什么现成的作品那样。

You could write a custom function or loadable UDF , but it would be a significant work, with significant impact on the database. 您可以编写一个自定义函数或可加载的UDF ,但这将是一项巨大的工作,对数据库有重大影响。 Then you could run WHERE GET_BID_ID(comment) < 4000 . 然后,您可以运行WHERE GET_BID_ID(comment) < 4000

What you can do more easily is devise some way of extracting the bid_id using available string functions . 可以更轻松地做的是设计一种使用可用字符串函数提取bid_id方法。

For example if the bid_id is always in the last ten characters, you can extract those, and replace all characters that are not digits with nil. 例如,如果bid_id始终位于最后十个字符中,则可以提取这些字符,并将所有非数字字符替换为nil。 What is left is the bid_id , and that you can compare. 而剩下的是bid_id你可以比较。

Of course you need a complex expression with LENGTH() , SUBSTRING() , and REPLACE() . 当然,您需要使用LENGTH()SUBSTRING()REPLACE()的复杂表达式。 If the bid_id is between easily recognizable delimiters, then perhaps SUBSTRING_INDEX() is more your friend. 如果bid_id在容易识别的分隔符之间,则SUBSTRING_INDEX()可能更适合您。

But better still... add an INTEGER column, initialize it to null, then store there the extracted bid_id . 但是最好还是...添加一个INTEGER列,将其初始化为null, 然后将提取的bid_id存储在bid_id Or zero if you're positive there's no bid_id . 或为零(如果您是肯定的),则没有bid_id Having data stored in mixed contexts is evil (and a known SQL antipattern to boot). 将数据存储在混合上下文中是有害的(并且要引导一个已知的SQL反模式)。 Once you have the column available, you can select every few seconds a small number of items with new_bid_id still NULL and subject those to extraction, thereby gradually amending the database without overloading the system. 一旦拥有了可用的列,就可以每隔几秒钟选择少量带有new_bid_id仍为NULL的项目, new_bid_id进行提取,从而逐步修改数据库而不会导致系统超载。

In practice 在实践中

This is the same approach one would use with more complicated cases. 这与在更复杂的情况下使用的方法相同。 We start by checking what we have (this is a test table) 我们首先检查一下我们拥有的(这是一个测试表)

SELECT commento FROM arti LIMIT 3;

+-----------------------------------------+
| commento                                |
+-----------------------------------------+
| This is the first comment 100 200 42500 |
| Another 7 Q 32768                       |
| And yet another 200 15 55332            |
+-----------------------------------------+

So we need the last characters: 所以我们需要最后一个字符:

SELECT SUBSTRING(commento, LENGTH(commento)-5) FROM arti LIMIT 3;
+-----------------------------------------+
| SUBSTRING(commento, LENGTH(commento)-5) |
+-----------------------------------------+
|  42500                                  |
|  32768                                  |
|  55332                                  |
+-----------------------------------------+

This looks good but it is not; 看起来不错,但事实并非如此; there's an extra space left before the ID. ID之前还有多余的空间。 So 5 doesn't work, SUBSTRING is 1-based. 因此5不起作用,SUBSTRING是基于1的。 No matter; 不管; we just use 4. 我们只用4。

...and we're done. ...我们就完成了。

mysql> SELECT commento FROM arti WHERE SUBSTRING(commento, LENGTH(commento)-4) < 40000;
+-------------------+
| commento          |
+-------------------+
| Another 7 Q 32768 |
+-------------------+

mysql> SELECT commento FROM arti WHERE SUBSTRING(commento, LENGTH(commento)-4) BETWEEN 35000 AND 55000;
+-----------------------------------------+
| commento                                |
+-----------------------------------------+
| This is the first comment 100 200 42500 |
+-----------------------------------------+

The problem is if you have a number not of the same length (eg 300 and 131072). 问题是如果您有一个不一样长的数字(例如300和131072)。 Then you need to take a slice large enough for the larger number, and if the number is short, you will get maybe "1 5 300" in your slice. 然后,您需要为较大的数字取一个足够大的切片,如果数字较短,则您的切片中可能会得到“ 1 5 300”。 That's where SUBSTRING_INDEX comes to the rescue: by capturing seven characters, from " 131072" to "1 5 300", the ID will always be in the last space separated token of the slice . 这就是SUBSTRING_INDEX抢救的原因:通过捕获从“ 131072”到“ 1 5 300”的七个字符,ID将始终位于slice的最后一个空格中

IN THIS LAST CASE , when numbers are not of the same length, you will find a problem. 在最后一种情况下 ,当数字的长度不同时,您会发现问题。 The extracted IDs are not numbers at all - to MySQL, they are strings . 提取的ID根本不是数字,对于MySQL来说,它们是字符串 Which means that they are compared in lexicographic, not numerical, order; 这意味着它们按字典顺序而不是数字顺序进行比较; and "17534" is considered smaller than "202", just like "Alice" comes before "Bob". 和“17534”被认为比“202” 更小 ,就像“爱丽丝”来“鲍勃”之前。 To overcome this you need to cast the string as unsigned integer, which further slows down the operations. 为了解决这个问题,您需要将字符串转换为无符号整数,这会进一步降低操作速度。

WHERE CAST( SUBSTRING(...) AS UNSIGNED) < 4000
select * from mysql_table_name where substring(comment,start,length, signed integer) < 4000

This will work, but I suggest create new column and put the bid value in it then compare. 这将起作用,但是我建议创建一个新列,并将出价值放入其中,然后进行比较。

To update value in new column you can use 要更新新列中的值,您可以使用

update table set newcol = substring(comment,start,length)

Hope this will help 希望这会有所帮助

Just get your bid_id column cleaned up. 只需清理您的bid_id列即可。 Then index is. 然后索引是。

create table `prior`
(   id int auto_increment primary key,
    comments text not null
);
insert `prior` (comments) values ('asdfasdf adfas d d 93827363'),('mouse cat 12345678');
alter table `prior` add column bid_id int; -- add a nullable int column
select * from `prior`; -- bid_id is null atm btw
update `prior` set bid_id=right(comments,8); -- this will auto-cast to an int
select * from `prior`; 
+----+-----------------------------+----------+
| id | comments                    | bid_id   |
+----+-----------------------------+----------+
|  1 | asdfasdf adfas d d 93827363 | 93827363 |
|  2 | mouse cat 12345678          | 12345678 |
+----+-----------------------------+----------+

Create the index: 创建索引:

CREATE INDEX `idxBidId` ON `prior` (bid_id); -- or unique index

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

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