fotobleu Posted April 2, 2008 Share Posted April 2, 2008 hi i have a db with a bunch of records, each record has a comma delimited tag str in it for ex: TAGS black, white, green yellow,white,red green,black,blue gray,orange,white what i want to do trough mysql is get the count for each tag occurrence so for the example above i would get TAG COUNT,TAG NAME 3 white 2 green 2 black 1 yellow 1 orange 1 red 1 blue 1 gray they dont have to be sorted i can do this in php, but im trying to put some of the work on the db, has anyone done this before with a sql statement thanks in advance -daniel Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/ Share on other sites More sharing options...
quiettech Posted April 2, 2008 Share Posted April 2, 2008 Check mysql documentation on 11.4 String Functions. You'll probably want a LIKE directive along with COUNT, if I understood your question right. SELECT COUNT(*) FROM `somewhere` WHERE `tag` LIKE '%white%'; Note however this will not work if 'white' can appear more than once on the same tag field. On that case, you will need to use regular expressions (11.4.2 on the manual). I'm not enirely sure how to do that with REGEXP. But I seem to believe you can get the count of a pattern matching in a string. Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/#findComment-507376 Share on other sites More sharing options...
gluck Posted April 2, 2008 Share Posted April 2, 2008 hi i have a db with a bunch of records, each record has a comma delimited tag str in it for ex: TAGS black, white, green yellow,white,red green,black,blue gray,orange,white what i want to do trough mysql is get the count for each tag occurrence so for the example above i would get TAG COUNT,TAG NAME 3 white 2 green 2 black 1 yellow 1 orange 1 red 1 blue 1 gray they dont have to be sorted i can do this in php, but im trying to put some of the work on the db, has anyone done this before with a sql statement thanks in advance -daniel DO you mean a field has the value "white,black,red" and you want each color listing? If that is the case I doubt you can do it. You migh want to select all values and then split in php. Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/#findComment-507983 Share on other sites More sharing options...
fotobleu Posted April 3, 2008 Author Share Posted April 3, 2008 yeah thats exactly what i mean, each field has a comma delimited keyword string thats what i thought too, but i wanted to ask to make sure, the problem is how do i differentiate between the keywords in each field and select each one and match each one, i guess you cant do it in mysql, ill do it in php, thanks Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/#findComment-508112 Share on other sites More sharing options...
aschk Posted April 3, 2008 Share Posted April 3, 2008 Indeed, unfortunately because the data is essentially unnormalised you can't extra this information "on the fly". Normalise this and you won't have a problem. Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/#findComment-508234 Share on other sites More sharing options...
fotobleu Posted April 4, 2008 Author Share Posted April 4, 2008 normalize, you mean split the field containing the comma separated keyword list into individual fields for each keyword, if thats what you mean, then i cant do that for my spec because i'm allowing the user to choose the nr of keyword it wants to enter, is this what you meant by normalize? thanks Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/#findComment-509085 Share on other sites More sharing options...
Barand Posted April 5, 2008 Share Posted April 5, 2008 Instead of a comma delimited fields in the table, create a second table for the keywords So, currently [pre] mytable ---+------------+----------------------+ id | somedata | tags | ---+------------+----------------------+ 1 | xyz | black, white, green | 2 | abc | yellow,white,red | ---+------------+----------------------+ [/pre] normalized [pre] mytable tags ---+------------+ +---+--------+------------+ id | somedata | |id | my_id | tag | ---+------------+ +---+--------+------------+ 1 | xyz | | 1 | 1 | black | 2 | abc | | 2 | 1 | white | ---+------------+ | 3 | 1 | green | | 4 | 2 | yellow | | 6 | 2 | white | | 7 | 2 | red | +---+--------+------------+ [/pre] Link to comment https://forums.phpfreaks.com/topic/99138-can-this-be-done-find-cound-of-keyword-occurence-over-multiple-records/#findComment-510202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.