SQLite 帮助(使用子查询连接表)

发布于 2024-11-07 14:11:11 字数 1203 浏览 4 评论 0原文

伙计们,我对这个感到疯狂...... 我有两个表:

1. UniquePrefixes
2. Operator

UniquePrefixes 仅包含“前缀”字段。 示例:

Prefix
------
1
12
123
12345

“Operator”表有大量数据,还包括“Prefix”字段。 示例:

..... Prefix ......
----- ------ ------
      1
      12

我想要实现的是:

(pseudo)

foreach unique 'prefix'
select the 'Prefix' from 'Operator' if is equal to a unique 'prefix'
OR
select the 'closest' match that fits into that

示例:

unique prefix = 1 (exists on 'Operator' so I am fine)
unique prefix = 12345 (doesnt exist on 'Operator' so I must get '12')

到目前为止我所做的是:

SELECT
    *
FROM
    UniquePrefixes
LEFT OUTER JOIN Operator
on 
    Operator.Prefix =(
        SELECT
            Operator.Prefix
        FROM
            Operator,
            UniquePrefixes
        WHERE
            length(Operator.Prefix)<= UniquePrefixes.prefix
        AND UniquePrefixes.prefix LIKE(
            Operator.Prefix || '%'
        )
        ORDER BY
            Operator.Prefix DESC
        LIMIT 1
    )

但它不起作用,因为子选择首先被执行(显然):(

我希望这是有道理的,并且非常感谢任何帮助

Guys I am going mad with this one...
I have two tables:

1. UniquePrefixes
2. Operator

UniquePrefixes contains 'Prefix' field only.
Example:

Prefix
------
1
12
123
12345

The 'Operator' table have a lot of data, including a 'Prefix' field as well.
Example:

..... Prefix ......
----- ------ ------
      1
      12

What I am trying to achieve is:

(pseudo)

foreach unique 'prefix'
select the 'Prefix' from 'Operator' if is equal to a unique 'prefix'
OR
select the 'closest' match that fits into that

Example:

unique prefix = 1 (exists on 'Operator' so I am fine)
unique prefix = 12345 (doesnt exist on 'Operator' so I must get '12')

What I have done so far is:

SELECT
    *
FROM
    UniquePrefixes
LEFT OUTER JOIN Operator
on 
    Operator.Prefix =(
        SELECT
            Operator.Prefix
        FROM
            Operator,
            UniquePrefixes
        WHERE
            length(Operator.Prefix)<= UniquePrefixes.prefix
        AND UniquePrefixes.prefix LIKE(
            Operator.Prefix || '%'
        )
        ORDER BY
            Operator.Prefix DESC
        LIMIT 1
    )

But it doesnt work since the subselect gets executed first (obviously) :(

I hope that this makes sense and will really appreciate any help

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

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

发布评论

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

评论(1

分开我的手 2024-11-14 14:11:11
    SELECT UniquePrefixes.Prefix, Operator.Prefix
      FROM UniquePrefixes
 LEFT JOIN Operator ON UniquePrefixes.Prefix LIKE Operator.Prefix || '%'
                   AND length(Operator.Prefix) <= length(UniquePrefixes.prefix)
     ORDER BY length(Operator.Prefix) desc
    SELECT UniquePrefixes.Prefix, Operator.Prefix
      FROM UniquePrefixes
 LEFT JOIN Operator ON UniquePrefixes.Prefix LIKE Operator.Prefix || '%'
                   AND length(Operator.Prefix) <= length(UniquePrefixes.prefix)
     ORDER BY length(Operator.Prefix) desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文