字符串的左连接

发布于 2024-09-13 05:57:32 字数 503 浏览 8 评论 0原文

在我的网站的一部分上,用户输入多个搜索词。这些搜索词中的每一个都可能返回 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 技术交流群。

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

发布评论

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

评论(3

墨落成白 2024-09-20 05:57:32

我认为这就是他想要的:

--结果--

Search Term____Title________________________________Author______Call No
cats           How to Care for Millions of Cats     JohnB       B.342
kittens        Why Kittens are your only Friends    Old Lady    L.4239
rabies         null                                 null        null

etc.

也许是这样的:

SELECT
  CASE count
    count > 0
      'Search Term', books.title 'Title', books.author 'Author', books.call_no 'Call No'
    count <= 0
      'Search term 1'
  END CASE
FROM
  (SELECT COUNT('Search term 1') count, 'Search term 1' 'Search Term' ,*
   FROM books
   WHERE books.title LIKE 'Search term 1');

(不过你必须解决这些问题)

I think this is what he wants:

--Results--

Search Term____Title________________________________Author______Call No
cats           How to Care for Millions of Cats     JohnB       B.342
kittens        Why Kittens are your only Friends    Old Lady    L.4239
rabies         null                                 null        null

etc.

Maybe something like this:

SELECT
  CASE count
    count > 0
      'Search Term', books.title 'Title', books.author 'Author', books.call_no 'Call No'
    count <= 0
      'Search term 1'
  END CASE
FROM
  (SELECT COUNT('Search term 1') count, 'Search term 1' 'Search Term' ,*
   FROM books
   WHERE books.title LIKE 'Search term 1');

(you'll have to work the kinks out though)

半夏半凉 2024-09-20 05:57:32

如果将数组值连接在一起,您可以通过这种方式使用全文搜索 (FTS):

   SELECT x.searchterm,
          b.*
     FROM (SELECT $s[0 ... 20] AS search_term) x
LEFT JOIN BOOKS b ON MATCHES(b.title) AGAINST (x.searchterm)

$s[0 ... 20] 是连接成单个字符串的搜索词的占位符,将每个术语都有一个空格字符。

LEFT JOIN 将确保您获得搜索词,如果没有匹配的内容,则其余列为 NULL。

You could use Full Text Search (FTS) this way, if you concatenate the array values together:

   SELECT x.searchterm,
          b.*
     FROM (SELECT $s[0 ... 20] AS search_term) x
LEFT JOIN BOOKS b ON MATCHES(b.title) AGAINST (x.searchterm)

$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.

伴我老 2024-09-20 05:57:32

连接条件与 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.

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