0

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:

  1. translate my php function into MYSQL form (it is a litter hard for me)
  2. 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?)
2
  • Why do you need this function and what would be wrong with just selecting those columns directly from tab1 ? Commented Apr 22, 2016 at 5:41
  • fun(@a,@b) maybe very complicated, for example I need $3*Log(@a+1)/EXP(@b)$ Commented Apr 22, 2016 at 5:44

4 Answers 4

1

You can call the function directly like this:

Select func(c1, c2) as r From tab1;

SQLFiddle

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

6 Comments

Yes, but the function must be defined in MYSQL then? In my case, I defined the function in php, and in that case fun(c1,c2) will not work.
How you can call a function from PHP in MySQL ?!!
To do that , You must return the results to PHP then enumerate over them calling the function you just defined. @vanabel
Try the second answer @vanabel
Yeah, I see, but it seem quite complicated. Especially I want to use the fun as a rank function to the result (in mysql ORDER BY fun).
|
1
public function func($param1, $param2) {
    // your code
}

$query = "Select c1, c2 From tab";
$stmt = $link->prepare($query);
if ($stmt->execute()) {
    while ($row = $stmt->fetch(PDO::FETCH_NAMED)) {
        func($row["c1"], $row["c2"]); 
    }
}

Comments

1

I'm not sure what the PHP function does or how it works so this may not be a perfect translation to a MySQL function but hopefully you can use it as a starting point.

DELIMITER //
CREATE FUNCTION FUN(d INT, t FLOAT)
RETURNS FLOAT DETERMINISTIC NO SQL
BEGIN
    IF d = 0 THEN
        RETURN 0;
    ELSE
        RETURN LOG(d)/LOG(10)+(UNIX_TIMESTAMP()-t)/45000;
    END IF;
END//
DELIMITER ;

1 Comment

I almost do the exactly the same translation, but then there is a problem (not you answer itself, but my original problem) How to initialize it in Laravel, please see Use user defnied function of mysql in Laravel5 in case you're interested in.
0

So you can do that:

Select IF(c1 = 0, 0, LOG(c1)/LOG(10)+(UNIX_TIMESTAMP()-c2)/45000) as r From tab1

SQLFiddle

Thanks to @Matt

2 Comments

Is there any possibility to make the constant 45000 as a vaiable that passed in by PHP? e.g. $T=45000, in fact, I defined it as an env constant in Laravel 5.
if you define your query directly from PHP, of course you can do that. like: $T=45000; $sql = "Select .... $T .... From tab1" then you call the query.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.