字符串的左连接
在我的网站的一部分上,用户输入多个搜索词。这些搜索词中的每一个都可能返回 0 行,或者返回几行。我对所有搜索执行 UNION 并获取行。
如果我可以获取响应中的所有搜索词,无论它们是否返回任何行,事情对我来说都会容易得多。有没有一种方法可以将字符串(在本例中为搜索词)左连接到 SELECT 查询?
更多信息更新:
我有一个带有“标题”字段的“书籍”表。
用户最多输入 10 个标题 search_terms,我当前对所有这些执行 UNION 以获取匹配的行。所以,就像
SELECT $s[0] AS Search_Term,* WHERE Title LIKE '%$s[0]%' UNION
SELECT $s[1] AS Search_Term,* WHERE Title LIKE '%$s[1]%' ...etc
我希望这样,如果给定的搜索没有结果,我仍然会得到 Search_Term 和 NULL 标题。
On part of my site, users enter multiple search terms. Each of these search terms may return 0 rows, or return several. I perform a UNION on all the searches and get the rows.
Thing are much easier for me if I can get all the search terms in the response, regardless of whether they return any rows. Is there a way to essentially LEFT JOIN a string (in this case the search term) to a SELECT query?
More information update:
I have a table Books with the field Title.
The user enters up to 10 title search_terms, and I currently perform a UNION on all these to get matching rows. So it's like
SELECT $s[0] AS Search_Term,* WHERE Title LIKE '%$s[0]%' UNION
SELECT $s[1] AS Search_Term,* WHERE Title LIKE '%$s[1]%' ...etc
I'd like it so that if there are no results for a given search, I still get the Search_Term back along with the NULL Title.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这就是他想要的:
--结果--
也许是这样的:
(不过你必须解决这些问题)
I think this is what he wants:
--Results--
Maybe something like this:
(you'll have to work the kinks out though)
如果将数组值连接在一起,您可以通过这种方式使用全文搜索 (FTS):
$s[0 ... 20]
是连接成单个字符串的搜索词的占位符,将每个术语都有一个空格字符。LEFT JOIN 将确保您获得搜索词,如果没有匹配的内容,则其余列为 NULL。
You could use Full Text Search (FTS) this way, if you concatenate the array values together:
$s[0 ... 20]
is a placeholder for the search terms concatenated into a single string, separating each term with a space character.The LEFT JOIN will ensure that you get your search term and NULL for the rest of the columns if nothing matches.
连接条件与 WHERE 条件一样,您可以使用“LIKE”或“=”根据您选择的条件进行连接。如果您需要更多详细信息,我需要一些查询示例和表定义。
Join conditions work just like WHERE conditions, you can use "LIKE" or "=" to join on the condition of your choice. If you need more detail, I'll need some query examples and table definitions.