views:

142

answers:

1

I have a query that makes several calls within a SELECT statement to a user-defined function. The function (vfget) returns the value back from key=value pairs contained within a string.

Is it possible for the query to just call the function once and store this in a variable so that it can be reused within the same query?

Currently my query is:

SELECT 
        CASE WHEN(right(vfget(appmod_params, 'service'), 3) = '_dd' OR right(vfget(appmod_params, 'service'), 3) = '_wp')
            THEN left(vfget(appmod_params, 'service'), length(vfget(appmod_params, 'service'))-3)
            ELSE vfget(appmod_params, 'service')
        END
FROM ota1003
LIMIT 10
+1  A: 
SELECT  @vf := vfget(appmod_params, 'service'),
        case when right(@vf, 3) IN ('_dd', '_wp')
            then left(@vf, length(@vf) - 3)
            else @vf
        end
from ota1003
limit 10

or this:

SELECT  case when right(vf, 3) IN ('_dd', '_wp')
            then left(vf, length(vf) - 3)
            else vf
        end
FROM    (
        SELECT  vfget(appmod_params, 'service') AS vf
        FROM    ota1003
        LIMIT 10
        ) q

Nested queries in MySQL are buffered and hence less efficient, though with only 10 records this should not be much of a problem.

Quassnoi
Thanks, worked perfectly after removing the extra "("
James
Is it possible to avoid having the first field output in the results?
James
@James: enclose the query into a nested query, though it will be less efficient.
Quassnoi
@Quassnoi Thanks I will try your suggestion.
James
@Quassnoi, it worked after referring to vf instead of @vf, thanks!
James