简体   繁体   English

是否有更好的方法来编写此查询

[英]is there a better way to write this query

I am trying to search and update record which start which a partcular number or fall with a range. 我正在尝试搜索和更新记录,其中包含一个部分数字或一个范围的下降。

I have made a query which works where values start with a particular number. 我做了一个查询,其中值以特定数字开头。 I can't figure out how to do it with a range. 我无法弄清楚如何使用范围来做到这一点。

for example I want to update the following values in products_ean with " " 例如,我想用products“更新products_ean中的以下值

255
201-230   ---> starts with 201,202,203 ...230
236
980-990   ---> starts with 980.981,982 ...990

I have written the following query which works but not sure if it is efficent espcialy when it has to serach over 100k records. 我写了下面的查询,但是当它必须搜索超过100k的记录时,不确定它是否有效。 It doesn't work with range. 它不适用于范围。

UPDATE products SET products_ean ="" 
where products_ean like "200%" 
OR products_ean like "020%" 
OR products_ean like "023%" 
OR products_ean like "027%" 
OR products_ean like "042%" 
OR products_ean like "221%" 
OR products_ean like "209%" 
OR products_ean like "041%" 
OR products_ean like "049%" 
OR products_ean like "026%" 
OR products_ean like "025%" 
OR products_ean like "299%";

This will be a full table scan anyhow, so you can use a function on products_ean with no loss of performance. 无论如何,这将是一个全表扫描,因此您可以在products_ean上使用函数而不会降低性能。 This said, you can get the query more readable, but probably not much faster. 这就是说,你可以让查询更具可读性,但可能不会更快。 However, you can still try whether it is faster, to take the three leading digits and compare these: 但是,你仍然可以尝试它是否更快,取三个前导数字并比较这些:

UPDATE products SET products_ean = '' 
where left(products_ean,3) in ('200', '020', '027', ...);

If you find it more readable, you can even use ranges: 如果您发现它更具可读性,您甚至可以使用范围:

UPDATE products SET products_ean = '' 
where left(products_ean,3) = '255'
   or left(products_ean,3) between '201' and '230'
...

maybe you could try this not sure about performance but less code.. if your ean is a string field. 也许你可以尝试这个不确定性能而不是代码..如果你的ean是一个字符串字段。 you can try this 你可以试试这个

UPDATE products SET products_ean ="" 
where (left(products_ean,3) between 201 and 230) or
(left(products_ean,3) between 980 and 990) or

PS: you might want to cast the field. PS:你可能想要施展这个领域。

I don't think its going to be faster but its another alternative: 我不认为它会更快,但它的另一种选择:

To Find: 寻找:

SELECT * 
FROM products
WHERE products_ean
REGEXP  '^(200|020|023|027|042|221|209|041|049|026|025|299)'

To Replace: 取代:

UPDATE products SET products_ean = ''
WHERE products_ean
REGEXP  '^(200|020|023|027|042|221|209|041|049|026|025|299)'

note: it will not use indexes 注意:它不会使用索引

You can try this 你可以试试这个

UPDATE products SET products_ean = " "
WHERE STRCMP(products_ean, '200') >= 0 AND STRCMP(products_ean, '220') <= 0

你可以尝试在哪里包含,它必须像:

WHERE CONTAINS(t.products_ean, '"200*" OR "020*" OR "023*"')

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

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