dagnasty Posted July 13, 2006 Share Posted July 13, 2006 My brain is running in circles and I'm going nuts over this one.I have a MySQL database with the row "searchterm" in it and I am looking for a way to pull the 10 most common search terms. I know.. I should have had a query that looked for a duplicate search result when it was put into the database, and added onto a counter. It would have made things a LOT easier at this point.Here's an example if my explanation wasn't too good:searchterm row:catdogdogcatcatmousemouse catdogWhat I am looking to output:cat = 4dog = 3mouse 2Thanks to anyone who can enlighten me.Database also contains a row id auto increment if that can help any. Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/ Share on other sites More sharing options...
brown2005 Posted July 13, 2006 Share Posted July 13, 2006 WELL UR BEST BET, AS FAR AS I KNOW, BUT IM NO EXPERT, IS TO CREATE A TABLE AND A SCRIPT THAT INSERTS EACH RESULT IN IT, AND DO IT THAT WAY Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57187 Share on other sites More sharing options...
dagnasty Posted July 13, 2006 Author Share Posted July 13, 2006 I have the script, and the database (with 40,000 rows) Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57193 Share on other sites More sharing options...
GingerRobot Posted July 13, 2006 Share Posted July 13, 2006 [code]<?<?php$sql = mysql_query("SELECT * FROM `your table`");while($row = mysql_fetch_array($sql)){ $search[$row[searchterm]]++;}print_r($search);?>[/code]With the data i chucked in to test this, outputtedArray ( [dog] => 3 [cat] => 2 [rabbit] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57195 Share on other sites More sharing options...
dagnasty Posted July 13, 2006 Author Share Posted July 13, 2006 you got lucky that it was sorted correctly. It's 7 Am and i've been up all night working on this project. Is there a quick way to sort the data?sort(); doesn't seem to do exactly what I want. Maybe I'm doing it wrong... It seems to print out only the number and not the value, and it also sorts from 1 and up.If you guys are willing to save me some time please feel free to help me out, if not, I'll wake up tomorrow and tackle the rest. My brain isn't working right. I'm going to bed. Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57222 Share on other sites More sharing options...
brown2005 Posted July 13, 2006 Share Posted July 13, 2006 SELECT *, COUNT(*) AS num FROM table ORDER BY num DESC LIMIT 10; Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57226 Share on other sites More sharing options...
bowenbowen Posted July 13, 2006 Share Posted July 13, 2006 SELECT COUNT(DISTINCT searchterm) AS `searchterm_count`, searchterm FROM table GROUP BY searchterm ORDER BY 'searchterm_count' DESC LIMIT 10Give that a go, that should give you your top ten search terms descending Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57227 Share on other sites More sharing options...
dagnasty Posted July 13, 2006 Author Share Posted July 13, 2006 No count is set for each search term in the database. or does "AS `searchterm_count`" not actually refer to a row?And wouldn't that just set you up to count the rows and not actually contain the actual row information from the database?Oh god i'm so tired. Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57229 Share on other sites More sharing options...
brown2005 Posted July 13, 2006 Share Posted July 13, 2006 nope COUNT(DISTINCT searchterm) is counting the things in the row and then searchterm_count is just naming it.... Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57235 Share on other sites More sharing options...
bowenbowen Posted July 13, 2006 Share Posted July 13, 2006 My bad sorryThis worked for meSELECT DISTINCT searchterm, COUNT(*) AS `Count` FROM search GROUP BY searchterm ORDER BY `Count` DESC LIMIT 10 Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57238 Share on other sites More sharing options...
brown2005 Posted July 13, 2006 Share Posted July 13, 2006 yeap like i said in my original post.. but if u have a group by i dont think u need the DISTINCT, well i dont know, maybe u do. lolglad it worked.. Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57239 Share on other sites More sharing options...
bowenbowen Posted July 13, 2006 Share Posted July 13, 2006 Yeah you are right, don't need the distinct --> tired mind here as well.....I am now lost on what you want though, I thought you needed the top ten search terms? Surely that SQL achieves this with the example you provided?Perhaps I getting wrong end of the stick or I am incredibly dum.....sorry if I am, because I have been wasting your time. Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57241 Share on other sites More sharing options...
brown2005 Posted July 13, 2006 Share Posted July 13, 2006 i dunno, im confused.. lol Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57244 Share on other sites More sharing options...
dagnasty Posted July 13, 2006 Author Share Posted July 13, 2006 $result = @mysql_query("SELECT DISTINCT searchquery, COUNT(*) AS `Count` FROM tscrapestats GROUP BY searchquery ORDER BY `Count` DESC LIMIT 10"); while($row = mysql_fetch_array($result)){ $search[$row[searchquery]]++; $amount[$row[Count]]++;}This works! (tested with print_r) However, I don't know how to print each item out correctly. :) I tried to$search[0] and search[0][0] and got nothing? What method do I use? Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57486 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 your array notation/usage is a little fudged up for your purposes. it should work fine if you go:[code]<?phpwhile (stuff = stuff){ $search["{$row['searchterm']}"]++;}?>[/code]this should yield an array of format $search[search_term] = searchterm_count. Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57498 Share on other sites More sharing options...
ShogunWarrior Posted July 13, 2006 Share Posted July 13, 2006 I believe:[code]SELECT DISTINCT searchterm,searchterm as st, (SELECT COUNT(*) FROM search WHERE search.searchterm=st) FROM search[/code][b]Edit: Tested.[/b] Quote Link to comment https://forums.phpfreaks.com/topic/14458-finding-most-searched-for-in-mysql-table/#findComment-57635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.