Using #39;LIKE#39; operator with a subquery that returns multiple results(将“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"运算符与返回多个结果的子查询一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将“LIKE"运算符与返回多个结果的子查询一起使用
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
