从一个表中查询出现在另一表的字段中的名称列表

编程入门 行业动态 更新时间:2024-10-27 16:36:01
本文介绍了从一个表中查询出现在另一表的字段中的名称列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想查询一个表的名称列表,该表显示在另一个表的字段中.

I want to query a list of names from one table that appear in a field in a different table.

示例:

table1.title>老虎伍兹作弊,老虎伍兹崩溃,布拉德·皮特很棒,麦当娜领养,布拉德·皮特拍电影

table1.title>Tiger Woods Cheats, Tiger Woods Crashes, Brad Pitt is Great, Madonna Adopts, Brad Pitt Makes a Movie

table2.names>泰格·伍兹,布拉德·皮特,麦当娜

table2.names>Tiger Woods, Brad Pitt, Madonna

这就是两个表和值.我想写一个查询来计算table2.names中哪个名称最常出现在table1.title中的查询

So those are the two tables and values. I would like to write a query that counts which names from table2.names appear most often in table1.title

有人建议使用内部联接,但是我无法使它正常工作……我感谢您的帮助!!谢谢.

Someone suggested using inner join, but I could not get it to work... I appreciate the help!! Thanks.

推荐答案

使用:

SELECT a.names, COUNT(b.titles) AS num FROM TABLE_2 a JOIN TABLE_1 b ON INSTR(b.title, a.names) > 0 GROUP BY a.names ORDER BY num DESC

请参阅有关 INSTR()的文档

See the documentation about INSTR() - checking for a value greater than 0 means the name occurred in the title, otherwise it would be zero.

AS num是列别名,您可以在ORDER BY中引用该别名,以升序或降序排序.

The AS num is a column alias, which you can reference in the ORDER BY to sort either in ASCending or DESCending order.

更多推荐

从一个表中查询出现在另一表的字段中的名称列表

本文发布于:2023-10-30 08:14:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现在   字段   名称   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!