简体   繁体   English

SQLite:如何通过部分列匹配进行JOIN?

[英]SQLite: How to JOIN with a partial column match?

I'm trying to get all the data from the Documents table where the list of Box numbers in the BoxNumber table is somewhere within a string in a column in the Documents table. 我试图从Documents表中获取所有数据,其中BoxNumber表中Box编号的列表位于Documents表中一列的字符串中。

The problem I'm running into right now is that none of the example code with '%' + ColumnName + '%' or '%' || ColumnName || '%' 我现在遇到的问题是没有带有'%' + ColumnName + '%' or '%' || ColumnName || '%'的示例代码。 '%' + ColumnName + '%' or '%' || ColumnName || '%' '%' + ColumnName + '%' or '%' || ColumnName || '%' will work. '%' + ColumnName + '%' or '%' || ColumnName || '%'将起作用。 it'll just return either nothing, or all the data in the Documents table. 它只会返回任何内容或“文档”表中的所有数据。

Example code: 示例代码:

This returns all data in the Documents table instead of all data containing a document number similar to the list in the BoxNumbers table. 这将返回“文档”表中的所有数据,而不是包含与BoxNumbers表中的列表相似的文档号的所有数据。

SELECT * 
from [Documents]
LEFT JOIN BoxNumbers ON 
BoxNumbers.BoxNum like '%' + Documents.DocNum + '%'

Anyone know why this isn't working? 有人知道为什么这行不通吗? It seems like it's worked for people using MySQL and SQL server, so is this just another quirk of SQLite? 似乎它适用于使用MySQL和SQL Server的人们,所以这仅仅是SQLite的另一个怪癖吗?

The SQLite concatenation operator is not + but || SQLite串联运算符不是+而是|| , so change to this: ,因此更改为:

SELECT * 
from [Documents]
INNER JOIN BoxNumbers ON 
BoxNumbers.BoxNum like '%' || Documents.DocNum || '%'

with INNER join. INNER加入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM