如何对字符串中的 ID 运行查询?

发布于 2024-09-25 16:22:43 字数 315 浏览 3 评论 0原文

我有一个包含此列的表 A:

  IDS(VARCHAR)
  1|56|23

我需要运行此查询:

  select TEST from TEXTS where ID in ( select IDS from A where A.ID = xxx )

TEXTS.ID 是一个 INTEGER。如何将字符串 A.IDS 拆分为多个整数以进行连接?

必须在 MySQL 和 Oracle 上运行。 SQL99首选。

I have a table A with this column:

  IDS(VARCHAR)
  1|56|23

I need to run this query:

  select TEST from TEXTS where ID in ( select IDS from A where A.ID = xxx )

TEXTS.ID is an INTEGER. How can I split the string A.IDS into several ints for the join?

Must work on MySQL and Oracle. SQL99 preferred.

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

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

发布评论

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

评论(1

雨巷深深 2024-10-02 16:22:43

首先,您不应该将这样的数据存储在列中。您应该将其拆分为一个单独的表,然后您就会有一个正常的连接,而不是这个问题。

话虽如此,您需要做的是以下操作:

  1. 将数字转换为字符串
  2. | (您的分隔符)字符填充它,在它之前和之后(我会告诉你为什么下面)
  3. 用相同的分隔符填充您要查找的文本,前后
  4. 执行 LIKE

这会运行缓慢!

这是执行您想要的 SQL (假设所有运算符和函数都在您的 SQL 方言中工作,您不会说这是什么类型的数据库引擎):

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
    JOIN A ON
        '|' + A.IDS + '|' LIKE '%|' + CONVERT(TEXTS.ID) + '|%'

您需要在前后用分隔符填充两者的原因是:如果您是寻找数字 5?您需要确保它不会仅仅因为它包含数字而意外地适合 56 数字。

基本上,我们会这样做:

... '|1|56|23|' LIKE '%|56|%'

如果 A 中只有 1 行,如果你这样做,它可能会运行得更快(但我不确定,你需要测量它):

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
WHERE
    (SELECT '|' + IDS + '|' FROM A) LIKE '%|' + CONVERT(TEXTS.ID) + '|%'

如果你的 A 中有很多行TEXTS 表中,值得努力添加代码来生成适当的 SQL,方法是首先从 A 表中检索值,使用 IN 构造适当的 SQL 并使用它:

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
WHERE
    ID IN (1, 56, 23)

这将运行得更快,因为现在它可以在此查询上使用索引。

如果您将 A.ID 作为一列,并将值作为单独的行,则执行查询的方式如下:

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
    INNER JOIN A ON TEXTS.ID = A.ID

这将比前一个查询运行得稍慢,但在前一个查询中,您必须首先检索 A.IDS,构建查询,并冒着生成必须编译的新执行计划的风险。

First of all, you should not store data like this in a column. You should split that out into a separate table, then you would have a normal join, and not this problem.

Having said that, what you have to do is the following:

  1. Convert the number to a string
  2. Pad it with the | (your separator) character, before it, and after it (I'll tell you why below)
  3. Pad the text you're looking in with the same separator, before and after
  4. Do a LIKE on it

This will run slow!

Here's the SQL that does what you want (assuming all the operators and functions work in your SQL dialect, you don't say what kind of database engine this is):

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
    JOIN A ON
        '|' + A.IDS + '|' LIKE '%|' + CONVERT(TEXTS.ID) + '|%'

The reason why you need to pad the two with the separator before and after is this: what if you're looking for the number 5? You need to ensure it wouldn't accidentally fit the 56 number, just because it contained the digit.

Basically, we will do this:

... '|1|56|23|' LIKE '%|56|%'

If there is ever only going to be 1 row in A, it might run faster if you do this (but I am not sure, you would need to measure it):

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
WHERE
    (SELECT '|' + IDS + '|' FROM A) LIKE '%|' + CONVERT(TEXTS.ID) + '|%'

If there are many rows in your TEXTS table, it will be worth the effort to add code to generate the appropriate SQL by first retrieving the values from the A table, construct an appropriate SQL with IN and use that instead:

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
WHERE
    ID IN (1, 56, 23)

This will run much faster since now it can use an index on this query.

If you had A.ID as a column, and the values as separate rows, here's how you would do the query:

SELECT
    TEXT   -- assuming this was misspelt?
FROM
    TEXTS  -- and this as well?
    INNER JOIN A ON TEXTS.ID = A.ID

This will run slightly slower than the previous one, but in the previous one you have overhead in having to first retrieve A.IDS, build the query, and risk producing a new execution plan that has to be compiled.

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