将“LIKE"运算符与返回多个结果的子查询一起使用

Using #39;LIKE#39; operator with a subquery that returns multiple results(将“LIKE运算符与返回多个结果的子查询一起使用)
本文介绍了将“LIKE"运算符与返回多个结果的子查询一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL 新手.请帮忙.

Newbie to SQL. Kindly help.

对于多个模式,我需要计算在其中一个字段中具有模式的记录数.我知道如何为一个模式执行此操作,但是当子查询有多个模式时,如何计算每个模式的计数.我正在使用甲骨文.我会尝试用一个例子来解释.

I need to count number of records which have a pattern in one of the fields, for multiple patterns. I know how to do it for one pattern, but how do I get count of each pattern when there are multiple patterns coming from a subquery. I am using Oracle. I will try to explain with an example.

SELECT count(*) FROM TableA
WHERE 
TableA.comment LIKE '%world%';

现在,此代码将返回 TableA.comment 字段中任何位置具有世界"的记录数.我的情况是,我有一个第二个查询,它返回一个模式列表,如世界".我如何获得每个模式的计数?

Now this code will return the number of records which have 'world' anywhere in the TableA.comment field. My situation is, I have a 2nd query which has returns a list of patterns like 'world'.How do I get the count of each pattern ?

我的最终结果应该是 2 列,第一列模式,第二列 count_of_pattern.

My end result should just be 2 columns, first column pattern, second column count_of_pattern.

推荐答案

可以使用like将子查询加入表中:

You can use like to join the subquery to the table:

SELECT p.pattern, count(a.comment)
FROM (subquery here that returns "pattern"
     ) p left outer join
     TableA a
     on a.comment like '%'||p.pattern||'%'
group by p.pattern;

这假定 pattern 没有通配符.如果是这样,那么您不需要进行连接.

This assumes that the pattern does not have wildcard characters. If it does, then you do not need to do the concatenation.

这也使用了 left outer join 以便返回所有模式,即使没有匹配.

This also uses a left outer join so that all patterns will be returned, even with no match.

这篇关于将“LIKE"运算符与返回多个结果的子查询一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)