簡體   English   中英

當字符串是使用 MySQL 的表中另一列中的數據時,如何在表的一列中搜索字符串

[英]How do I search one column of a table for a string when the string is the data in another column in that table using MySQL

我有一張表格,其中包含我們知識庫中的知識文章信息。

為簡單起見,它包含以下列:

articleID, title, knowledge_base, state, article_content

state是文章的工作流狀態,其中我只關心發布的版本,文章內容是文章的html。

我們正在刪除knowledge_base2,但是knowledge_base1 中有鏈接到這些文章的文章,我需要找到並刪除這些鏈接。

鏈接包含文章的文章 ID,所以我認為這很容易。

我想在 article_content 列中搜索對 Knowledge_base2 的 articleID 的任何引用。 我使用了這個代碼:

/* Any knowledge_base1 articles that link to an article in knowledge_base2. */
Select a.articleID, a.title
FROM table1 a
Join (SELECT articleID 
    From table1 b
    Where state = 'Published' 
    AND knowledge_base LIKE 'Knowledge_base2') b
ON a.articleID = b.articleID
WHERE a.knowledge_base = 'Knowledge_base1'
AND a.state = 'Published'
AND a.`article_content` LIKE concat('%',b.articleID,'%')
ORDER BY a.articleID

當我運行它時,我沒有得到任何結果,但沒有錯誤。 我知道 Knowledge_base1 中的鏈接引用了 Knowledge_base2 中的那些文章 ID,因此應該有很多結果。 我認為這條線就是我錯了:“等。 article_content LIKE CONCAT(‘%’,b.number,‘%’)”有沒有更好的方式來寫這個? 請注意,我對該數據庫具有只讀權限,無法使用本地臨時表。

表中數據的示例如下所示:

| articleID |         title          | knowledge_base  |   state   |       article_content                   |
+-----------+------------------------+-----------------+-----------+-------------
| KB0001    | How to fix a widget    | knowledge_base1 | Published | http://sp?id=kb_article_view&sysparm_article=KB0002 |
| KB0002    | How to make a sandwich | knowledge_base2 | Published | Random HTML content                                 |
| KB0003    | How to buy a car       | knowledge_base1 | outdated  | http://sp?id=kb_article_view&sysparm_article=KB0004 |
| KB0004    | House FAQs             | knowledge_base2 | Published | Random HTML content                                 |
+-----------+------------------------+-----------------+-----------+-------------

最后評論:經過更多的研究和測試后,我發現這似乎給了我所需的答案:

Select a.articleID, a.title
FROM table1 a
Join (SELECT articleID 
    From table1 b
    WHERE knowledge_base LIKE 'Knowledge_base2') b
ON a.article_content LIKE concat('%',b.articleID,'%')
WHERE a.knowledge_base = 'Knowledge_base1'
AND a.state = 'Published' 
ORDER BY a.articleID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM