pf » Related Posts
April 08, 2005
Related Posts

As you may have noticed I recently added tags to my blog. The main reason I implemented tags was so that I could find related posts based on mutual tags. So if two posts have similar tags, they will show up in as related.
Here's how I find the related tags:
<cfquery datasource="#ds#" name="tags"> SELECT tag FROM blogtags WHERE entryid = <cfqueryparam cfsqltype="cf_sql_integer" value="#url.id#"> </cfquery> <cfif tags.recordcount> <cfquery datasource="#ds#" name="related"> SELECT e.title, e.id, COUNT(t.entryid) FROM blogentries AS e, blogtags AS t WHERE t.entryid <> <cfqueryparam cfsqltype="cf_sql_integer" value="#url.id#"> AND t.tag IN (<cfqueryparam list="true" value="#ValueList(tags.tag)#">) AND e.id = t.entryid GROUP BY e.title, e.id HAVING COUNT(t.entryid) > 1 ORDER BY COUNT(t.entryid) DESC LIMIT 5 </cfquery> ...display related entries </cfif>
I know, I'm using two database queries, and this could probably be done using one query, but I already need to run the tags query to list the tags for the current entry.
The query pulls the 5 most related entries. In order for them to be related I require at least two common tags, that's what the HAVING COUNT(t.entryid) > 1 is for.
Need Help With SQL?
SQL & Database Training Classes - Instructor Led Online Training Classes for SQL, MySQL, Oracle, and SQL Server.
Permalink | Add Comment |
add to del.icio.us
| Tags: tags, sql, cfquery, databases, blog, folksonomy, tagging
add to del.icio.us
| Tags: tags, sql, cfquery, databases, blog, folksonomy, tagging
Related Entries
- Make your blog better with tags - October 14, 2005
- How To Make a Tag Cloud - June 24, 2005
- Visualization of my del.icio.us tags - April 24, 2006
- Amazon Adds Tagging - November 15, 2005
- Increase Productivity with Action Tagging - August 1, 2005
Trackback Address: 315/BAB0994DB837E2D8ADB2A7652A10C411
Comments
On 04/11/2005 at 9:22:03 AM MDT foO wrote:
1
ahhhhhhh, thanks for posting about it, pete! appreciated. was curious if you designated the tags when you posted the blog entry, or had some kinda script/regex that automatically pulled it from the entry... or something like that.
thx again ;)
On 06/16/2005 at 3:46:05 PM MDT since1968 wrote:
2
Pete, thanks for posting this, very helpful. This mostly works in MySQL, but needs slight tweaking. If you don't assign an alias to the COUNT() value, MySQL returns an invalid group by error. But if you do something like "COUNT(t.entryid) AS weight" and then change your ORDER BY clause to "ORDER BY weight DESC" it works fine.
- ColdFusion 8 FCKeditor Vulnerability
- Ajax Same Origin Policy No More with Firefox 3.5
- Firefox 3.5 Introduces Origin Header, Security Features
- Tips for Secure File Uploads with ColdFusion
- 7 Years And Blog Entry Number 700
- CFCatch Java Exceptions
- Cheat Sheet for SQL Server
- CFML on Google App Engine for Java
Subscribe to my RSS Feed:
RSS
RSS

Pete Freitag is a software engineer, and web developer located in










