简体   繁体   中英

SQL - Delimiting to a Specific Range

SELECT * FROM `cpus_z-series` WHERE `Model_ID` LIKE '%Z5%';

The above SQL query would target both 500 and 5000 series. I want to know if there's a way to delimit this further to select only 500 or only 5000 series models. No matter how many percentage signs I add to the end, the same happens.

I'm assuming this might be possible using RegEx, but I'm not that great with regular expressions.

Edit:

I forgot to mention that the model numbers overlap. For example, Z550 exists, but so does Z5500. And furthermore, there are model numbers with awkward endings, like Z552 and Z5505.

First, why do you have the wildcard at the front? Wouldn't this do the same thing?

WHERE `Model_ID` LIKE 'Z5%';

If so, you can just use the length of the string:

WHERE `Model_ID` LIKE 'Z5%' AND LENGTH(Model_ID) = 4

Of course, you can also use regular expressions. However, the advantage of using this method is that it can actually take advantage of an index on Model_Id , because the LIKE pattern starts with a fixed character.

Also, you can use _ to represent any single character in like :

WHERE `Model_ID` LIKE 'Z5__'

This is probably closest to what you want to do.

To select only 5xx models

SELECT * 
FROM `cpus_z-series` 
WHERE `Model_ID` REGEXP '^Z5[0-9]{2}$'

you can limit it using _ underscore

SELECT * FROM cpus_z-series WHERE Model_ID LIKE '%Z5__';

two underscore for hundred and 3 underscore for thousand

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