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. Link to comment https://forums.phpfreaks.com/topic/123965-sql-statement-for-top-searched-keywords/ 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 Link to comment https://forums.phpfreaks.com/topic/123965-sql-statement-for-top-searched-keywords/#findComment-639916 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... Link to comment https://forums.phpfreaks.com/topic/123965-sql-statement-for-top-searched-keywords/#findComment-639972 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 Link to comment https://forums.phpfreaks.com/topic/123965-sql-statement-for-top-searched-keywords/#findComment-639992 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. Link to comment https://forums.phpfreaks.com/topic/123965-sql-statement-for-top-searched-keywords/#findComment-640148 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.