SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    List of tags, highlight relevant ones - possible in one query?

    For a blog script I'm creating I have a table called blog_post and a table called tag. The structure of each is as follows (irrelevant fields removed):

    Code:
    blog_post
      id
      title
      body
      date
    
    tag
      tag
      post_id
    For a list of all tags that have been added, I'd use SELECT DISTINCT tag FROM tag, and for a list of all tags that apply to a certain post I'd use SELECT tag FROM tag WHERE post_id = 123, but I'm looking to combine the two, so for a specified post I can return a list of all tags, highlighting the ones that apply to the current post.

    I could do this quite easily in PHP with two SELECT queries and some simple array logic, but I was wondering if there was a single query I could use, which would return two columns, the first being the tag, and the second being, say, a boolean value indicating whether it applies to the post or not. Can this be done? I'm using MySQL 4.1.22.

    Thanks!

    Sam

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by SJH View Post
    ... for a specified post I can return a list of all tags, highlighting the ones that apply to the current post.
    Code:
    SELECT t.tag
         , p.post_id
      FROM ( SELECT DISTINCT tag FROM tag ) AS t
    LEFT OUTER
      JOIN tag AS p
        ON p.tag = t.tag
       AND p.post_id = 123
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Rudy, that works like a charm


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •