Saturday, 17 February 2018

Solution: How to get single column value returning from mysql function 1

Are you using MySQL function? Is it returning any single value? Then you may confused, how to get that value from record set after executing query. You will get idea after reading this detailed post.

Okay... Let we write one simple function named mytest(), and make it returns TRUE. Then we will see how to get and assign that TRUE to our server side script variable. 
DELIMITER $$
CREATE  FUNCTION `mytest`() RETURNS char(100) CHARSET utf8
BEGIN
RETURN 'TRUE';

END$$
DELIMITER ;
To run this function we need to use this below query.
SELECT mytest();
After running this query, We will get an out put like this below screenshot.

MySQL function return




So, We can't consider mytest() as column name when your are trying to get the value TRUE from returned record set because of that parenthesis. Correct?

To solve this we are just going to assign Alice name to that function.
SELECT mytest() AS returnval;
After running this query, We will get an out put like this below screenshot.





Now, We can use returnval as a column name and we can able to get it's value TRUE from returned record set.

Have any doubt??? Feel free to comment here!!!

1 comment: