1

I have a simple table that logs simple events an administrator does. Nothing complicated.

One of the columns is userid, so I can reference the user's name from the users table:

SELECT u.username FROM `users` u, `history` h WHERE u.userid = h.userid

My issue is that the internal system also logs events (just like a user), but there is no userid related to the internal system. It is simply logged as zero, in the record.

Is there a way to modify the query to reflect this? I've tried a LEFT JOIN, but that doesn't let me specify "system" as the username. The other thing i can do is enter a new user into the user's table, and call it "Internal System". But I really don't want to do that.

Is it possible to do this from the query level?

1 Answer 1

1

Use:

   SELECT COALESCE(u.username, 'SYSTEM') AS username,
     FROM HISTORY h
LEFT JOIN USERS u ON u.userid = h.userid

COALESCE will return the first non-NULL value, so if you LEFT JOIN onto USERS and username is NULL--then "system" or whatever you prefer will appear.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.