Mysql 当使用类似于x的“%”时,组_concat返回空,但不使用类似于x的“%”时,组_concat返回空
在下面的示例查询中,我提取与关键字匹配的所有文本,并按年份和月份分割结果。我更喜欢搜索“text”——所有字符串的具体版本,而不是只搜索带有WHERE子句的每个字段,但have不会返回任何数据:Mysql 当使用类似于x的“%”时,组_concat返回空,但不使用类似于x的“%”时,组_concat返回空,mysql,Mysql,在下面的示例查询中,我提取与关键字匹配的所有文本,并按年份和月份分割结果。我更喜欢搜索“text”——所有字符串的具体版本,而不是只搜索带有WHERE子句的每个字段,但have不会返回任何数据: SELECT date_format(created_date, '%Y-%m') as yr_mo, count(id), group_concat(coalesce(title,''),coalesce(story,'') SEPARATOR ' ') as text from input_for
SELECT
date_format(created_date, '%Y-%m') as yr_mo,
count(id),
group_concat(coalesce(title,''),coalesce(story,'') SEPARATOR ' ') as text
from input_form
where dupe = 0 /*and story like '%flood%'*/
group by yr_mo
having text like '%flood%'
order by yr_mo
下面是我取消注释/**和类似“%flood%”的故事并删除HAVING子句时返回的结果:
yr_mo count(id) text
2011-04 2 Floods Roads in my community one time proved to b...
2011-05 21 THE TRUSTED LADY. It was during my usual visits ...
2011-06 22 HEAVY RAINFALL On our village we were affected wit...
2011-07 52 FEED THE CHILDREN PROGRAMME(1) The world food prog...
2011-08 29 I was saved and helped to prosperI am one of the v...
2011-09 15 FLOODS In the past three months the country have f...
2011-10 19 FLOODFlood is a very bad disaster at the same time...
2011-11 9 RESPONDING TO DISASASTERUNICEF landed over relief ...
2011-12 3 EARLY PREVENTION OF FLOODShaving sensed the likeli...
2012-01 44 HUNGER STRIKE In Wataalamu village the...
2012-04 8 THE FALLEN BRIDGEThe Kambu bridge along the Mombas...
2012-05 7 Increasing earth's natural environment awarenessAf...
2012-06 4 A misery solvedRecently 10 people died in my commu...
2012-07 21 Lsot HopesWhen the Agricultural Farmers Society ca...
我尝试过用coalescestory避免空值,但结果是一样的
编辑1:我发现一个更长更混乱的查询可以工作,并提供我想要的:
SELECT
date_format(created_date, '%Y-%m') as yr_mo,
count(if( ( story like '%hunger%' OR title like '%hunger%' ) and (story_location_city like '%nairobi%' OR story_location_neighborhood like '%nairobi%'),id,null)) as 'hunger',
count(if( ( story like '%poverty%' OR title like '%poverty%' ) and (story_location_city like '%nairobi%' OR story_location_neighborhood like '%nairobi%'),id,null)) as 'poverty',
count(if( ( story like '%school%' OR title like '%school%' ) and (story_location_city like '%nairobi%' OR story_location_neighborhood like '%nairobi%'),id,null)) as 'school',
count(id) as total
from input_form
where dupe = 0
group by yr_mo
order by yr_mo
如果必须,我可以搜索If语句中的每个字段,它将提供特定的结果。没有比这更有效的方法了吗?,因此HAVING子句要求字符串flood出现在连接文本的前1024个字符内,而WHERE子句只要求它出现在input\u form.story中的某个位置
即使不是因为截断,我认为GROUP_CONCAT+HAVING也不是您真正想要的,因为这将包括任何月份的所有记录,其中有任何匹配的记录。例如,如果2012年4月有20条记录,但只有10条记录包含洪水,那么,如果不进行截断,则具有方法的组_CONCAT+将表示2012年4月有20条匹配记录,并且将包含所有这些记录的文本。我已经采纳了您的意见,并编写了一种替代方法,该方法应仅匹配每个月内包含关键字的记录。您的COUNTIF。。。这个方法在我看来很不错。但如果您愿意,可以使用连接这些字段。布尔模式下的COUNTIFMATCHstory反饥饿贫困学校如何?您可以添加“或匹配标题…”来搜索标题或故事中的所有单词。注意,这只适用于MyISAM,应该有一个全文索引。