virtuexru Posted September 12, 2008 Share Posted September 12, 2008 OK, so when someone searches on my site, I add the keyword to a DB. Now I want to show the results as by the "Top 10 most searched keywords".. so my SQL to insert is this: $kcount = "INSERT INTO search (id, keyword) VALUES ('', '$keyword')"; mysql_query($kcount); so say I have "hummer" 3 times and "porsche" 4 times and "toyota" once. How would I make an SQL statement to see the most used keyword (in this case, it would be porsche)? I know this is probably really simple but I'm drawing a blank. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 12, 2008 Share Posted September 12, 2008 SELECT DISTINCT keyword, COUNT(*) AS count FROM search GROUP BY keyword ORDER BY count DESC LIMIT 1 Although it would be better if your table was like this table search id, keyword, count 1, hummer, 3 2, porsche, 4 3, toyota, 1 Quote Link to comment Share on other sites More sharing options...
fenway Posted September 12, 2008 Share Posted September 12, 2008 First, you don't need distinct, since it's already group-by'ed. Second, you can easily accomplish "hit counters" with and INSERT... ON DUPLICATE KEY... UPDATE... Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 12, 2008 Share Posted September 12, 2008 I'm always lost with distinct... I tested this query in my database and it seemed to me that DISTINCT was necessary Quote Link to comment Share on other sites More sharing options...
fenway Posted September 12, 2008 Share Posted September 12, 2008 I'm always lost with distinct... I tested this query in my database and it seemed to me that DISTINCT was necessary Shouldn't be... one count per keyword. Quote Link to comment 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.