Jump to content

Find most frequently word in field


eevan79

Recommended Posts

How to find most frequently word in table field?

I have table players with filed items and want to find most used word in that filed.

 

Items are like 'HIDU', 'I099', 'I05F'. Thats item code (id) that is used for images. I also have table items where is filed itemid same as items in players table. In that table I have additional information about that item. So how to find one frequently used item in from players table ?

Link to comment
Share on other sites

This field contain 4 letters code like 'H45T', 'AB7T' etc. Also I have images H45T to match selected item. So I want to find most used item in filed items. There are lots of records with different or same items code, so I want to find what same items are most used.

 

This can be very hard job for MySql cause it need to compare every filed :). Maybe this is impossible to do in this way.

 

Link to comment
Share on other sites

Hi

 

Is it a 4 character field? Or is it a field that contains multiple 4 character fields? It multiple are the separated by a space, a comma or what?

 

It is is a single 4 character field it is easy. Multiples would be far more difficult (and also suggest an issue with the database design).

 

Can you give us the table declaration and a few sample records?

 

All the best

 

Keith

Link to comment
Share on other sites

Fileds:

`id`, `botid`, `gameid`, `colour`, `kills`,

`deaths`, `creepkills`, `creepdenies`, `assists`, `gold`, `neutralkills`,

`item1`, `item2`, `item3`, `item4`, `item5`, `item6`,

`hero`, `newcolour`, `towerkills`, `raxkills`, `courierkills`

Values:

(8, 0, 1, 7, 10, 6, 279, 0, 12, 24988, 31, 'I0BG', 'I0BJ', 'I095', 'I0A9', 'I06B', 'I099', 'O00J', 7, 4, 4, 0),

(9, 0, 1, 1, 10, 11, 333, 3, 10, 6748, 49, 'I06B', 'I0AB', 'I0BA', 'I0G1', 'I0AI', 'I09C', 'E005', 1, 1, 0, 0),

(10, 0, 1, 8, 6, 12, 154, 6, 14, 1, 3, 'I04I', 'I0GJ', 'I000', 'I0BK', '\0\0\0\0', 'I05I', 'Hjai', 2, 0, 0, 0),

(11, 0, 2, 3, 0, 0, 0, 0, 0, 316, 0, 'I02O', '\0\0\0\0', '\0\0\0\0', '\0\0\0\0', '\0\0\0\0', '\0\0\0\0', 'H00S', 3, 0, 0, 0)

 

\0\0\0\0 - This is empty/blank item and no need to count.

All items have 4 characters and it's not separated by a space, comma...

 

item1 filed is slot 1 for item

item2 is for slot 2...

There are 6 slots and I want to find most used item for each slot.

Let's start for item 1...then it'll be easy to find for other slots.

Link to comment
Share on other sites

i suggest that you keep track of the different combinations in a separate table, incrementing a count column each time one is encountered. performing the count on the fly will take more time and resources, and become more time- and resource-intensive as the database grows.

Link to comment
Share on other sites

I know, but it's only possible if hosting program (which is used for importing data) modify to update another table.

Maybe solution is to periodically update count for items table via cron (or some script). Then I need to check for about 300 items.

 

Only problem is that items is chained with heroes (another table). My goal is to check most used items for each hero. :)

 

So I dont need items count table, but hero_items and update data for every filed (hero_items.item1, hero_items.item2 etc.) where I match most used item.

 

I'm not sure which is best way to do something like this.

Link to comment
Share on other sites

Hi

 

Really would be easier if you had them on a seperate table.

 

If not then something like this

 

 

SELECT 'item1' AS ItemType, item1 AS Item, count(*) AS ItemCountFROM someTableGROUP BY ItemType, ItemUNIONSELECT 'item2' AS ItemType, item2 AS Item, count(*) AS ItemCountFROM someTableGROUP BY ItemType, ItemUNIONSELECT 'item3' AS ItemType, item3 AS Item, count(*) AS ItemCountFROM someTableGROUP BY ItemType, ItemUNIONSELECT 'item4' AS ItemType, item4 AS Item, count(*) AS ItemCountFROM someTableGROUP BY ItemType, ItemUNIONSELECT 'item5' AS ItemType, item5 AS Item, count(*) AS ItemCountFROM someTableGROUP BY ItemType, ItemUNIONSELECT 'item1' AS ItemType, item6 AS Item, count(*) AS ItemCountFROM someTableGROUP BY ItemType, Item

 

 

All the best

 

Keith

Link to comment
Share on other sites

Ok, I have tried your code but it gives me following result:

ItemType -> item1

Item -> blank (nothing)

ItemCount -> 3105

This is my code:

 

SELECT 'item1' AS ItemType, item1 AS Item, count(*) AS ItemCount     FROM dotaplayers     GROUP BY ItemType, Item     UNION     SELECT 'item2' AS ItemType, item2 AS Item, count(*) AS ItemCount     FROM dotaplayers     GROUP BY ItemType, Item     UNION     SELECT 'item3' AS ItemType, item3 AS Item, count(*) AS ItemCount     FROM dotaplayers     GROUP BY ItemType, Item     UNION     SELECT 'item4' AS ItemType, item4 AS Item, count(*) AS ItemCount     FROM dotaplayers     GROUP BY ItemType, Item     UNION     SELECT 'item5' AS ItemType, item5 AS Item, count(*) AS ItemCount     FROM dotaplayers     GROUP BY ItemType, Item     UNION     SELECT 'item6' AS ItemType, item6 AS Item, count(*) AS ItemCount     FROM dotaplayers     GROUP BY ItemType, Item

 

 

$getItems = $db->query($sql);     $rowItems = $db->fetch_array($getItems,'assoc');      echo $rowItems['ItemType'];      echo $rowItems['Item'];      echo $rowItems['ItemCount'];

 

Link to comment
Share on other sites

I found solution. It's not so hard.

Here is code:

SELECT 
    count(*) as total,
    item1 FROM dotaplayers 
    WHERE hero = '$heroid'
    GROUP BY item1 having count(*) > 1 
    ORDER BY total DESC LIMIT 5

Thats for item1 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.