SQL:如何比较两个单词的各个部分?

发布于 2024-11-03 19:22:57 字数 311 浏览 0 评论 0原文

我有带有 ID 列的表 1 和带有 ID 号列的表 2。 ID 格式例如为“aac557z”。我想按编号比较这些 ID。 表 1:

在此处输入图像描述

和表 2:

在此处输入图像描述

我想从 table1 中选择列 ID 中单词为“567”的行。数字可能不同。 也许有人有想法? 我正在使用 MS Access 2010。 提前致谢。

I have table1 with ID column and table2 with ID number column. ID format for example is "aac557z". I want to compare those ID's by the piece where is number.
Table1:

enter image description here

And Table2:

enter image description here

I want to select rows from table1 where the piece of the word is "567" in column ID. Numbers may be different.
Maybe someone has an idea?
I'm using MS Access 2010.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-10 19:22:57

如果 ID 的数字部分始终位于相同位置且长度相同,则可以使用 MID 函数获取数字部分进行比较。在大桌子上它不会很快。

以下是语法链接:http://office.microsoft.com/access-help/mid-function-HA001228881.aspx" microsoft.com/en-us/access-help/mid-function-HA001228881.aspx

如果您要经常进行此比较,最好在表中添加另一个字段并用插入表时 ID 的数字部分。然后使用该字段进行比较,您的查询应该会更快。

If the numeric part of the ID is always in the same place and the same length you can use the MID function to get the number part for the comparison. It's won't be very fast on large tables.

Here's a link to the syntax: http://office.microsoft.com/en-us/access-help/mid-function-HA001228881.aspx

If you are going to do this comparison often it might be better to add another field to your table and populate it with the numeric part of the ID when it is inserted in to the table. Then using that field for your comparison your query should be faster.

浪漫之都 2024-11-10 19:22:57

您可以使用用户定义的函数从 Id 中提取数字部分,然后其余部分就相当简单了。

这是一个完成这项工作的函数:

CREATE FUNCTION ExtractInteger(@String VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE @Count INT
DECLARE @IntNumbers VARCHAR(1000)
SET @Count = 0
SET @IntNumbers = ''

WHILE @Count <= LEN(@String)
BEGIN
IF SUBSTRING(@String,@Count,1) >= '0'
AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + SUBSTRING(@String,@Count,1)
END
SET @Count = @Count + 1
END

RETURN @IntNumbers
END

然后进行比较,如下所示:

SELECT * from TABLE1
JOIN TABLE2
    on dbo.ExtractInteger(TABLE1.ID) = dbo.ExtractInteger(Table2.ID)

如果表非常大,那么这将是一个昂贵的查询!

You could use a user-defined function to extract the number part from the Id and then the rest is fairly trivial.

Here's a function to do the job:

CREATE FUNCTION ExtractInteger(@String VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE @Count INT
DECLARE @IntNumbers VARCHAR(1000)
SET @Count = 0
SET @IntNumbers = ''

WHILE @Count <= LEN(@String)
BEGIN
IF SUBSTRING(@String,@Count,1) >= '0'
AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + SUBSTRING(@String,@Count,1)
END
SET @Count = @Count + 1
END

RETURN @IntNumbers
END

And then to compare, something like this:

SELECT * from TABLE1
JOIN TABLE2
    on dbo.ExtractInteger(TABLE1.ID) = dbo.ExtractInteger(Table2.ID)

If the tables are very large, then this is going to be an expensive query!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文