繁体   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