Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get invoking user ID (only) from MySQL

Tags:

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

like image 391
Dan Avatar asked Feb 01 '12 20:02

Dan


1 Answers

Manual page says:

SELECT SUBSTRING_INDEX(USER(),'@',1);
like image 135
Niloct Avatar answered Oct 04 '22 15:10

Niloct