现有表 test,<del>字段 ct 包含 aa,ab,abc,bc,c ;字段 no 包含 1,2,3,4,5 。</del> 如下:
| no | ct |
|---|---|
| 1 | aa |
| 2 | ab |
| 3 | abc |
| 4 | bc |
| 5 | c |
select ct,count(no) from test where ct like '%a%' or ct like '%b%' group by ct
| ct | count(no) |
|---|---|
| aa | 1 |
| ab | 1 |
| abc | 1 |
| bc | 1 |
而我想要实现结果是
| count(no) | |
|---|---|
| a | 3 |
| b | 3 |
<del>于是我在想新建一个数组,包含 a,b 。结果行不通。</del>
然后想到新建临时表 test1 字段 ct1 包含 a,b,如下
| ct1 |
|---|
| a |
| b |
select test1.ct1,count(no) from test,test1 where test.ct like test1.ct1 group by test1.ct1
select 'a',count(no) from test where ct like '%a%' union all select 'b',count(no) from test where ct like '%b%'
1
c6h6benzene 2021 年 4 月 8 日
其实你应该只是要看这些字符串 INSTR(ct, 'a')>0 和 INSTR(ct, 'b')>0 的数量?
|
2
vtea OP @c6h6benzene 举的例子是这样,实际并不是,因为字符很多,所以很是麻烦
|