0

In MYSQL, how do you assign a field name fname from the table ftable to a variable within a trigger query?

I am aware of mysql global variables: @@
Local Variables? @

But is that even the right syntax?

1 Answer 1

2

You can just assign it from the field right in the select; it would get the last selected value if several rows are returned.

SELECT @tmp:=fvalue FROM ftable WHERE ...

A simple SQLfiddle here.

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

3 Comments

Actually lets assume we don't know what the field value is. We only know a field name. My question confused that, let me fix it. Assuming it is field name not field value.. Will that statement result in a variable @tmp having the value of fname that I can use in the rest of the trigger script?
@SofianeMerah If fvalue is a field name, @tmp will have the value of that field. That is, if ftable has only one row where the field value of fvalue is 1, @tmp will contain the value 1.
hmm I tried using @tmp as a variable in INSERT INTO @tmp VALUES (8). But it didn't work. @tmp was assigned the value table2. I get a schema creation error.

Your Answer

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