www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > SQL

    SQL For all Structured Query Language, and general database questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-04-2009, 11:25 AM
    Justin's Avatar
    Justin Justin is offline
    Registered User
     
    Join Date: Apr 2003
    Location: Vermont (USA)
    Posts: 344
    resolved [RESOLVED] Tag cloud, counting across tables.

    I am trying to build a tag cloud list for a page I'm making and need a little bit of help. I know how I could do this with a few loops and a bunch of queries but would prefer a nicer solution.

    I have two tables, a tagmap that maps the tag id to a file id, and a tag table that maps tag ids to names. What I am currently doing is selecting all the tags from the table and displaying them, what I would like to do is also add the count of records that have that tag id in the tagmap table.

    I would like to select tags.id, tags.name, and a count of tag_id found in the tagmap table.

    tagmap
    Code:
    +----+---------+--------+
    | id | file_id | tag_id |
    +----+---------+--------+
    | 25 |       4 |      6 | 
    | 15 |       3 |      6 | 
    | 14 |       3 |      3 | 
    | 18 |       4 |      8 | 
    | 26 |       4 |      7 | 
    | 19 |       3 |      9 | 
    | 20 |       4 |     10 | 
    | 21 |       5 |     11 | 
    +----+---------+--------+
    tags
    Code:
    +--------+-----------+
    | tag_id | name      |
    +--------+-----------+
    |      3 | test      | 
    |      6 | rit       | 
    |      7 | calendar  | 
    |      8 | dates     | 
    |      9 | classes   | 
    |     10 | important | 
    |     11 | passwords | 
    +--------+-----------+
    In this example the query would return a count of 2 for tag rit (tag_id 6), and one for the others.

    How I can implement it is to just select all the records from the tags table and then loop over the results asking for counts. Is there a nicer way to do this with sql?
    Reply With Quote
      #2  
    Old 11-06-2009, 01:12 PM
    ssystems ssystems is offline
    Registered User
     
    Join Date: Oct 2009
    Posts: 246
    Code:
    SELECT
    	t1.id
    	,t1.tag_count,
    	t2.name
      FROM
    	tags t2
      JOIN
    	(SELECT COUNT(id) as tag_count, id FROM tagmaps GROUP BY id) As t1
    	ON
    	  t2.tag_id = t1.tag_id
    __________________
    Good Luck

    Santos Systems
    Reply With Quote
      #3  
    Old 11-08-2009, 12:39 PM
    Justin's Avatar
    Justin Justin is offline
    Registered User
     
    Join Date: Apr 2003
    Location: Vermont (USA)
    Posts: 344
    Thank you, just needed a few tweaks with field names but now it works!

    Thanks again, that was exactly what I needed.

    Code:
    SELECT
    	t1.tag_id,
    	t1.tag_count,
    	t2.name
    FROM
    	tags t2
    JOIN
    	(SELECT COUNT( id ) AS tag_count, tag_id FROM tagmap GROUP BY tag_id) AS t1
    	ON
    	   t2.tag_id = t1.tag_id
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:20 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.