php - Count occurrences of all strings using the colums value as parameter - mysql -
i'm gathering data information project, see words repeat more in database using column values search parameters, like:
table_product id | name | description 1 | blue shirt | cool shirt 2 | yellow pants | pretty nice pants 3 | red shirt | cool red shirt
the expected result like:
field | count shirt | 4 cool | 2 pants | 2 ... | ...
is there way achieve using querys? thanks.
edit: can't pre-set word list, goal cross string , count occurrences.
to type of result have union each word separate query. so..
select 'shirt' field, sum( case when locate('shirt', `name`) > 1 , locate('shirt', `description`) 2 when locate('shirt', `name`) > 1 or locate('shirt', `description`) 1 else 0 end ) 'count' table_product union select 'cool' field, sum( case when locate('cool', `name`) > 1 , locate('cool', `description`) 2 when locate('cool', `name`) > 1 or locate('cool', `description`) 1 else 0 end ) 'count' table_product union select 'pants' field, sum( case when locate('pants', `name`) > 1 , locate('pants', `description`) 2 when locate('pants', `name`) > 1 or locate('pants', `description`) 1 else 0 end ) 'count' table_product
recommendation:
i recommend make new column each count want.. in other words use pivoted result.. it'll easier handle , if trying result in programming language key name aka 'shirt', 'cool'... , value count.. demonstration
select sum( case when locate('shirt', `name`) > 1 , locate('shirt', `description`) 2 when locate('shirt', `name`) > 1 or locate('shirt', `description`) 1 else 0 end ) 'shirt', sum( case when locate('cool', `name`) > 1 , locate('cool', `description`) 2 when locate('cool', `name`) > 1 or locate('cool', `description`) 1 else 0 end ) 'cool', sum( case when locate('pants', `name`) > 1 , locate('pants', `description`) 2 when locate('pants', `name`) > 1 or locate('pants', `description`) 1 else 0 end ) 'pants' table_product;
Comments
Post a Comment