在 SQLite 中查询

发布于 2024-10-21 04:13:05 字数 917 浏览 2 评论 0原文

“IN”查询不起作用。如果我错了,请指导我。

KaizenResultsInformationTable 是具有字段“recordinfo”的 MasterTable,该字段包含子表 ID 作为字符串。 kaizenResultsRecordInformationTable 是具有字段“recordId”的子表。

我必须匹配孩子的记录。

查询:

select recordinfo from KaizenResultsInformationTable

输出:;0;1;2;3;4;5;6;7;8;9;10

查询

select substr(replace(recordinfo,';','","'),3,length(recordinfo))   
from KaizenResultsInformationTable`

:输出:"0","1","2","3","4", “5”

此查询无效:

select * from kaizenResultsRecordInformationTable 
where substr(recordid,0,2) in (
    select substr(replace(recordinfo,';','","'),3,length(recordinfo))
    from KaizenResultsInformationTable
)

此查询有效:

select * from kaizenResultsRecordInformationTable 
where substr(recordid,0,2) in ("0","1","2","3","4","5")

"IN" query is not working. Please guide me if i am wrong.

KaizenResultsInformationTable is MasterTable having field "recordinfo", this field contains Child table Ids as string.
kaizenResultsRecordInformationTable is Childtable having field "recordId".

I have to match records of child.

Query:

select recordinfo from KaizenResultsInformationTable

Output: ;0;1;2;3;4;5;6;7;8;9;10

Query:

select substr(replace(recordinfo,';','","'),3,length(recordinfo))   
from KaizenResultsInformationTable`

Output: "0","1","2","3","4","5"

This query is not working:

select * from kaizenResultsRecordInformationTable 
where substr(recordid,0,2) in (
    select substr(replace(recordinfo,';','","'),3,length(recordinfo))
    from KaizenResultsInformationTable
)

This query is working:

select * from kaizenResultsRecordInformationTable 
where substr(recordid,0,2) in ("0","1","2","3","4","5")

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

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

发布评论

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

评论(1

極樂鬼 2024-10-28 04:13:05

你不能像这样使用 in 。在第二个查询中,您将传入一个包含逗号分隔值列表的字符串。

最好将 ID 列表表示为每个值的一条记录。

另外,我不确定你为什么要获取 recordid 的子字符串。您通常应该为每一列存储一个值。

但是,如果您无法更改架构,则可以使用“like”而不是“in”进行字符串匹配。像这样的东西应该可以工作:

select a.* from kaizenResultsRecordInformationTable a
join KaizenResultsInformationTable b 
  on (';'+b.recordinfo+';') LIKE ('%;'+trim(substr(recordid,0,2))+';%')

因此,如果您的 recordinfo 看起来像 1;2;3;4;5;6 ,并且您的 substr(recordid,0,2 ) 看起来像 1,这将包括该行,如果 ";1;2;3;4;5;6;"就像“%;1;%”,这是真的。

You can't use in like that. In your second query, you are passing in a single string containing a comma-separated list of values.

It is better to represent a list of IDs as one record for each value.

Also, I'm not sure why you are taking a substring of your recordid. You should usually be storing one value per column.

However, if you can't change the schema, you can use string matching with 'like' instead of 'in'. Something like this should work:

select a.* from kaizenResultsRecordInformationTable a
join KaizenResultsInformationTable b 
  on (';'+b.recordinfo+';') LIKE ('%;'+trim(substr(recordid,0,2))+';%')

So if your recordinfo looks like 1;2;3;4;5;6, and your substr(recordid,0,2) looks like 1, this will include that row if ";1;2;3;4;5;6;" LIKE "%;1;%", which is true.

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