How can I store the result of a function into a variable?

Martin AJ

Please take a look at it:

SELECT calculate_both_score_and_reputation(u.id) AS sr FROM users u

It returns something like this:

+----------+
|    sr    |
+----------+
| 0,1322   |
| 3, 232   |
| -2, 9978 |
| 31, 5745 |
+----------+

Now I want to split the result of that function into two different columns:

SELECT SUBSTRING_INDEX(calculate_both_score_and_reputation(u.id),',', 1) AS s,
       SUBSTRING_INDEX(calculate_both_score_and_reputation(u.id),',', -1) AS r,
FROM users u

Here is the result:

+----+------+
| s  |   r  |
+----+------+
| 0  | 1322 |
| 3  | 232  |
| -2 | 9978 |
| 31 | 5745 |
+----+------+

The result is as expected. But as you see, that function will be called twice. Now I want to know, how can I call that function once and store the result in a variable, then parse that variable?

Giorgos Betsos

You can simply place your query in a subquery and consume the value returned by the function in the outer query:

SELECT SUBSTRING_INDEX(sr,',', 1) AS s,
       SUBSTRING_INDEX(sr,',', -1) AS r
FROM (
 SELECT calculate_both_score_and_reputation(u.id) AS sr 
 FROM users u) AS t

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I store a result of a function in a variable in Python?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

how can i store the printing result, obtained by "for statement" variable?

From Dev

Store function result into variable

From Dev

Store function result into variable

From Dev

How can I store the result of .each() into localStorage?

From Dev

Can I store the result of Fetch in a global variable using JavaScript?

From Dev

Store into variable result of function VBA

From Dev

PHP - How can I store variable states?

From Dev

How can I store the output of summary in a variable?

From Dev

How can I store a knex query in a variable?

From Dev

How can I store dataset() in Application variable?

From Dev

How can I store my variable in a file?

From Dev

how can executorService store result of task `i` in `array[i]`?

From Dev

How do I store a mongodb query result in a variable?

From Dev

how I can call from batch file a java script function with 2 arguments and return the result to a dos variable

From Dev

How to store a query result in a variable

From Dev

How can I store reference to the result of an operation in Go?

From Dev

How can i store the result of SRANDMEMBER with a COUNT argument in a SET?

From Dev

How can i store the result of SRANDMEMBER with a COUNT argument in a SET?

From Dev

How can I store the result of MongoDB query in a text file with Nodejs?

From Dev

Store function result to variable for later use in PHP?

From Dev

PHP: Store Result of JavaScript Function into a PHP Variable

From Dev

How I can use result of first function in other function

From Dev

How can I log the result of a function that returns a function?

From Dev

How do I use a function result to call a variable name?

From Dev

How can I use a function variable in a .then?

From Dev

How can i apply jquery function on a variable?

From Dev

how can i pass variable in parameter as a function?

Related Related

  1. 1

    How can I store a result of a function in a variable in Python?

  2. 2

    How can I store a result of a function in a variable in Python?

  3. 3

    how can i store the printing result, obtained by "for statement" variable?

  4. 4

    Store function result into variable

  5. 5

    Store function result into variable

  6. 6

    How can I store the result of .each() into localStorage?

  7. 7

    Can I store the result of Fetch in a global variable using JavaScript?

  8. 8

    Store into variable result of function VBA

  9. 9

    PHP - How can I store variable states?

  10. 10

    How can I store the output of summary in a variable?

  11. 11

    How can I store a knex query in a variable?

  12. 12

    How can I store dataset() in Application variable?

  13. 13

    How can I store my variable in a file?

  14. 14

    how can executorService store result of task `i` in `array[i]`?

  15. 15

    How do I store a mongodb query result in a variable?

  16. 16

    how I can call from batch file a java script function with 2 arguments and return the result to a dos variable

  17. 17

    How to store a query result in a variable

  18. 18

    How can I store reference to the result of an operation in Go?

  19. 19

    How can i store the result of SRANDMEMBER with a COUNT argument in a SET?

  20. 20

    How can i store the result of SRANDMEMBER with a COUNT argument in a SET?

  21. 21

    How can I store the result of MongoDB query in a text file with Nodejs?

  22. 22

    Store function result to variable for later use in PHP?

  23. 23

    PHP: Store Result of JavaScript Function into a PHP Variable

  24. 24

    How I can use result of first function in other function

  25. 25

    How can I log the result of a function that returns a function?

  26. 26

    How do I use a function result to call a variable name?

  27. 27

    How can I use a function variable in a .then?

  28. 28

    How can i apply jquery function on a variable?

  29. 29

    how can i pass variable in parameter as a function?

HotTag

Archive