I have a table with ID column
ID column is like this : IDxxxxyyy x will be 0 to 9
I have to select row with ID like ID0xxx% to ID3xxx%, there will be around 4000 ID with % wildcard from ID0000% to ID3999%.
It is like combining LIKE with IN
Select * from TABLE where ID in (ID0000%,ID0001%,...,ID3999%)
I cannot figure out how to select with this condition.
If you have any idea, please help.
Thank you so much!
You can use pattern matching with LIKE
. eg
WHERE ID LIKE 'ID[0-3][0-9][0-9][0-9]%'
Will match an string that:
ID
)[0-9][0-9][0-9]
) This is not likely to perform well at all. If it is not too late to alter your table design, I would separate out the components of your Identifier
and store them separately, then use a computed column to store your full id eg
CREATE TABLE T
(
NumericID INT NOT NULL,
YYY CHAR(3) NOT NULL, -- Or whatever type makes up yyy in your ID
FullID AS CONCAT('ID', FORMAT(NumericID, '0000'), YYY),
CONSTRAINT PK_T__NumericID_YYY PRIMARY KEY (NumericID, YYY)
);
Then your query is a simple as:
SELECT FullID
FROM T
WHERE NumericID >= 0
AND NumericID < 4000;
This is significantly easier to read and write, and will be significantly faster too.
这应该这样做,它将获得所有以 IDx 开头的 ID,x 从 0 到 4
Select * from TABLE where ID LIKE 'ID[0-4]%'
你可以试试 :
Select * from TABLE where id like 'ID[0-3][0-9]%[a-zA-Z]';
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.