I think this a pretty classical setting: Suppose I have a table tab1, with columns c1, c2. Then I want to select c1, c2 as variable of a function fun(a,b) in another select:
SELECT fun(@a,@b) as r FROM (SELECT @a:=c1, @b:=c2 FROM tab1) AS tab ORDER BY r LIMIT 10;
The fun is pre-defined in PHP:
function fun($d, $t){
$timenow = time();
$perd = 45000;
$di = (int)$d;
$tf = (float)$t;
if ($di === 0)
{
return 0;
}else{
return (Log($di)/Log(10)+($timenow-$tf)/$perd);
}
}
The problem is that the variable is not updated at all. I build a test environment in http://sqlfiddle.com/#!9/9ffd6, the fun(@a,@b) is replaced by @a for simplicity.
UPDATE
It seems that my original question canot properly describe my problem. And I updated it, thanks to @wajeeh, the solution could be:
- translate my php function into MYSQL form (it is a litter hard for me)
- Use php function, but return the mysql results as array. (In that case I need to write the odering function by hand! and then (maybe) need another SELECT of MYSQL?)
tab1?fun(@a,@b)maybe very complicated, for example I need $3*Log(@a+1)/EXP(@b)$