SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast daliren's Avatar
    Join Date
    Oct 2001
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    placing primary address first

    I have 2 tables, user and address. Since a user can have multiple addresses, I designate one as primary in user (user.primaryAddressId->address.addressId). When displaying addresses, I always want the primary address to be first. The basic query is:
    Code:
     SELECT user.userId, user.primaryAddressId, a.address
     FROM user, address
     WHERE address.userId = user.userId
    Since there could be multiple addresses, how can I word the query, presumedly in an ORDER BY clause, to return first the primary address, then the others?
    cheers,

    Darren Cassidy

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    order by case when user.primaryAddressId = address.addressId then 0 else 1 end
    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
  •