Dan
Dan

Reputation: 5231

Get invoking user ID (only) from MySQL

I'm new to MySQL, so this may be a dumb question, but I haven't found an answer specificly after googling and digging here on SO.

All I'm attempting to do is get the user ID, and only the user ID, of the invoking user of an insert or update on a given table. I'm not asking for any help with a trigger in this question, so don't worry about that.

SELECT user()

Gives: user@[IP Address]

SELECT current_user()

Gives: user@%

Minor problem: I only want user

My Question:

Am I going about this the wrong way?

Is there a cleaner, or more readable way to get just user rather than having to use something likesubstr(user(),1,instr(user(),'@')-1) ? I've written some triggers in Oracle where I could just use the USER value for what I need.

Thanks in advance

Upvotes: 5

Views: 276

Answers (2)

Ben Lee
Ben Lee

Reputation: 53349

The docs specifically say to use SUBSTRING_INDEX. See http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_user

Quote:

You can extract only the user name part like this:

mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
        -> 'davida'

Upvotes: 3

Niloct
Niloct

Reputation: 10015

Manual page says:

SELECT SUBSTRING_INDEX(USER(),'@',1);

Upvotes: 4

Related Questions