SQL Query to sort based on count

Discussion in 'MySQL' started by forumposters, Apr 3, 2007.

  1. #1
    I have a list of tagids and I want to select images and sort them based on how many tags they have that match the list of tagids.

    I have a query like this:

    SELECT ImageID
    FROM Image,
    WHERE JobID = #arguments.JobID#"

    What do I need to put in the ORDER BY clause so that this query will sort the imageids in this way?
     
    forumposters, Apr 3, 2007 IP
  2. ThreeGuineaWatch

    ThreeGuineaWatch Well-Known Member

    Messages:
    1,489
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    140
    #2
    You need to alter your SELECT statement and COUNT the occurrences of each TagID (if I have understood correctly)
     
    ThreeGuineaWatch, Apr 3, 2007 IP
  3. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How are the tagIDs link to the imageID? Is it in the same table or a second table? How does the jobID relate to this?

    Here is a sample statement assuming the following table structure:

    tblImage - this table represents the images, one row per image
    -imageID int (pk)
    -imageName char

    tblTag - this table represents the tags, one row per tag
    -tagID int (pk)
    -tagName char

    tblImageTags - this table relates the images to the tags, there could be several rows for each image or tag
    -relationID int (pk)
    -imageID int (fk)
    -tagID int (fk)

    Select imageID, (select COUNT(tblImageTags.tagID) FROM tblImageTags WHERE tblImageTags.imageID = tblImage.imageID) AS tagCount
    FROM tblImage
    WHERE xxxxx
    ORDER BY tagCount

    I don't know if MySQL lets you do that, but MS-SQL does.
     
    druidelder, Apr 3, 2007 IP
  4. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    i think mysql does too but the requirement seems a little confuding though
     
    iama_gamer, Dec 14, 2010 IP