SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select all categories with LEFT JOIN

    Hi,

    I feel as if this should be straight forward, but my left join brain isn't working today. Here is the table structure:

    |Category|
    ----------
    |CatID |
    |CatName|

    |Cat2User|
    ----------
    |UserID |
    |CatID |

    I need to get a list of all the categories, and know which one a specific user is subscribed to.

    Any help is greatly appreciated.

    - Justin

  2. #2
    SitePoint Addict phpster's Avatar
    Join Date
    Feb 2005
    Location
    Toronto, Canada
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try

    select a.catname, b.userID from caegory a left outer join cat2userb on a.catid = b.cat_id
    phpster

    I wish my computer would do what I want it to.
    Not what I tell it to do...

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    select C.CatID
         , C.Category
         , case when CU.UserID = 937
                then 'this one!'
                else '' end     as which_one
      from Category as C
    left outer
      join Cat2User as CU
        on C.CatID = CU.CatID
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •