Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 计算给定列中具有字符A、B、C的条目数_Mysql_Sql - Fatal编程技术网

Mysql 计算给定列中具有字符A、B、C的条目数

Mysql 计算给定列中具有字符A、B、C的条目数,mysql,sql,Mysql,Sql,如果数据库表中有一列的值类似于“a.B”、“C.D”、“a.C”,我如何使用sql查询找出包含“a”、“B”、“C”或“D”(按字符分组)的条目数 我希望结果是: _________________________ | Characters| COUNT(*) | |*************************| | A | 10 | |*************************| | B | 15 | |

如果数据库表中有一列的值类似于“a.B”、“C.D”、“a.C”,我如何使用sql查询找出包含“a”、“B”、“C”或“D”(按字符分组)的条目数

我希望结果是:

 _________________________
|  Characters| COUNT(*)   |
|*************************|
|    A       |    10      |
|*************************|
|    B       |    15      |
|************|************|
|    C       |     8      |
|************|************|
|    D       |     17     |
|____________|____________|

同时包含“A”和“B”(如“A.B”)的列的条目应同时包含在“A”和“B”中。

您可以使用模式匹配来完成此操作。这里有一种方法:

select pattern.name, count(t.column)
from (select '%A%' as pattern, 'A' as name union all
      select '%B%' as pattern, 'B' as name union all
      select '%C%' as pattern, 'C' as name union all
      select '%D%' as pattern, 'D' as name
     ) patterns left join
     t
     on t.column like patterns.pattern
group by pattern.name;

注意,这使用一个子查询为模式定义一个“派生表”。这种精确的语法可能不适用于所有数据库,但类似的语法应该可以使用。

您可以使用模式匹配来实现这一点。这里有一种方法:

select pattern.name, count(t.column)
from (select '%A%' as pattern, 'A' as name union all
      select '%B%' as pattern, 'B' as name union all
      select '%C%' as pattern, 'C' as name union all
      select '%D%' as pattern, 'D' as name
     ) patterns left join
     t
     on t.column like patterns.pattern
group by pattern.name;
注意,这使用一个子查询为模式定义一个“派生表”。这种精确的语法可能不适用于所有数据库,但类似的语法应该可以使用。

给您:

create table my_table (
  characters varchar(100)
);

insert into my_table (characters) values ('A.B');
insert into my_table (characters) values ('C.D');
insert into my_table (characters) values ('A.C');

select 'A' as letter, count(*) from my_table where characters like '%A%'
union select 'B', count(*) from my_table where characters like '%B%'
union select 'C', count(*) from my_table where characters like '%C%'
union select 'D', count(*) from my_table where characters like '%D%';
结果:

letter  count(*)
------  --------
A              2
B              1
C              2
D              1
给你:

create table my_table (
  characters varchar(100)
);

insert into my_table (characters) values ('A.B');
insert into my_table (characters) values ('C.D');
insert into my_table (characters) values ('A.C');

select 'A' as letter, count(*) from my_table where characters like '%A%'
union select 'B', count(*) from my_table where characters like '%B%'
union select 'C', count(*) from my_table where characters like '%C%'
union select 'D', count(*) from my_table where characters like '%D%';
结果:

letter  count(*)
------  --------
A              2
B              1
C              2
D              1

用你正在使用的数据库标记你的问题。用你正在使用的数据库标记你的问题。