In my example query below, I am pulling all text that matches a keyword and splitting results by year-month. I prefer to search on ‘text’ – the concated version of all strings, instead of just against each field with a WHERE clause, but HAVING does not return any data:
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
Below is what gets returns when I uncomment /** and story like ‘%flood%’ **/ and remove the HAVING clause:
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...
I’ve tried avoiding nulls with coalesce(story,”) but the result was the same.
EDIT #1: I found a longer and messier query to work, and provide me what I want:
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 I must, I can search each field within an if() statement, and it will provide specific results. Is there no more efficient way to do this?
GROUP_CONCATtruncates its output to (by default) 1024 characters, so theHAVINGclause requires that the stringfloodappear within the first 1024 characters of the concatenated text, whereas theWHEREclause merely requires that it appear somewhere ininput_form.story.Even if it weren’t for the truncation, I think that
GROUP_CONCAT+HAVINGwould not really be what you would want, since that would include all records for any month that had any matching record. For example, if April 2012 had twenty records, but only ten containedflood, then aGROUP_CONCAT+HAVINGapproach would (if not for the truncation) say that there were twenty matching records for April 2012, and would include text from all of them.