tags:

views:

24

answers:

1

Hi,

i have 2 tables called user, with the fileds user.userID and user.lastActivityTime, and session with the same fields. Now i want to get an user by ID but if there is an entry in the sessionTable with the same userID i want to get all from user but the lastActivityTime from session. How can I do this?

Thank You

+1  A: 

SELECT * FROM user LEFT JOIN session ON user.UserID=session.UserID

And use session.lastActivityTime preferentially if it is not null.

Borealid
but i want to use this in WHERE and ORDER BY too, how can i do this?
Chris
@Chris: You can do a WHERE restriction and an ordering on either side of the join. Read up on SQL JOIN syntax.
Borealid
okay, thank you
Chris