[英]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
I have one table that contains knowledge article information in our knowledge base.我有一张表格,其中包含我们知识库中的知识文章信息。
For simplicity, it contains the following columns:为简单起见,它包含以下列:
articleID, title, knowledge_base, state, article_content
The state is the workflow state of the article, of which I only care about the published version and article content is the html for the articles. state是文章的工作流状态,其中我只关心发布的版本,文章内容是文章的html。
We are removing knowledge_base2, but there are articles in knowledge_base1 that link to those articles and I need to find and remove those links.我们正在删除knowledge_base2,但是knowledge_base1 中有链接到这些文章的文章,我需要找到并删除这些链接。
The links contain the articleID of the article, so I thought this would be easy.链接包含文章的文章 ID,所以我认为这很容易。
I want to search the article_content column for any reference to the articleIDs of knowledge_base2.我想在 article_content 列中搜索对 Knowledge_base2 的 articleID 的任何引用。 I used this code:
我使用了这个代码:
/* 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
When I run this I get no results but no errors.当我运行它时,我没有得到任何结果,但没有错误。 I know there are links in Knowledge_base1that reference those article IDs in Knowledge_base2 so there should be plenty of results.
我知道 Knowledge_base1 中的链接引用了 Knowledge_base2 中的那些文章 ID,因此应该有很多结果。 I assume this line is where I am wrong: "AND a.
article_content
LIKE concat('%',b.number,'%')" Is there a better way to write this?我认为这条线就是我错了:“等。
article_content
LIKE CONCAT(‘%’,b.number,‘%’)”有没有更好的方式来写这个? Note, I have read only permissions to this database, and am not able to use local temp tables.请注意,我对该数据库具有只读权限,无法使用本地临时表。
An example of the data in the table would look like this:表中数据的示例如下所示:
| 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 |
+-----------+------------------------+-----------------+-----------+-------------
Final comment: After much more research and testing This was what I found that appeared to give me the answer I needed:最后评论:经过更多的研究和测试后,我发现这似乎给了我所需的答案:
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.