SQLite 帮助(使用子查询连接表)
伙计们,我对这个感到疯狂...... 我有两个表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)