简体   繁体   English

选择大量数据,如 SQL server

[英]Select a large volume of data with like SQL server

I have a table with ID column我有一个带有 ID 列的表

ID column is like this : IDxxxxyyy x will be 0 to 9 ID 列是这样的: IDxxxxyyy x 将是 0 到 9

I have to select row with ID like ID0xxx% to ID3xxx%, there will be around 4000 ID with % wildcard from ID0000% to ID3999%.我必须选择 ID 为 ID0xxx% 到 ID3xxx% 的行,大约有 4000 个 ID 带有 % 通配符,从 ID0000% 到 ID3999%。

It is like combining LIKE with IN就像把 LIKE 和 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 .您可以将模式匹配与LIKE eg例如

WHERE ID LIKE 'ID[0-3][0-9][0-9][0-9]%'

Will match an string that:将匹配一个字符串:

  1. Starts with ID ( ID )以 ID ( ID ) 开头
  2. Then has a third character that is a number between 0 and 3 [0-3]然后有第三个字符,它是 0 到 3 [0-3] 之间的数字
  3. Then has 3 further numbers ( [0-9][0-9][0-9] )然后还有 3 个数字( [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如果现在改变你的表格设计还为时不晚,我会把你的Identifier的组件分开并单独存储,然后使用计算列来存储你的完整 ID,例如

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

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

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