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

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.

相关问题 如何在MySQL的一个表的一列上进行搜索调用? - How do I do a search call on one column of a table in MySQL? MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? MySql表类型,如何将列数据从一个表自动绘制到另一个表? - MySql table type, how do I draw column data from one table to another automatically? 如何将一个表中一列的数据除以另一表中一列的数据? - How do I divide data from a column in one table by data from a column in another table? MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql? 如何从一个mysql表INSERT INTO到另一个表并设置一列的值? - How do I INSERT INTO from one mysql table into another table and set the value of one column? 仅当使用MySQL在一个表中的列值与另一表相同时,才如何从两个表返回数据? - How to return data from two tables only when the column value in one table is same as another table using MySQL? 使用带有字符串的额外列将数据从一个Mysql表传输到另一个 - Transfer data from one Mysql table to another with an extra column having a string 如何将一个MySql表中的值与PHP中另一个表的列名匹配? - How do I match value from one MySql table to column name of another table in PHP? 从一个表的列到另一表的列的mysql数据减法 - mysql data substraction from column of one table to column of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM