0

can Mysql return multiple cell in 1 string ??

in sqlserver I can make function like this

CREATE FUNCTION [dbo].[getTribunByOrder]
(
    @idtrorder varchar(10)
)
RETURNS varchar(1000)
AS
BEGIN
    declare @trtribun varchar(1000)
    set @trtribun = ''
    select  @trtribun = @trtribun + nametribun +'='+ convert(varchar(3),jumlah) + ','
            from trtribun b
            where idtrorder = @idtrorder
    set @trtribun = substring(@trtribun, 0, len(@trtribun))
    RETURN @trtribun
END

return of this function = "name1=1,name2=3,name3=2"
can mysql create function like this ??

2
  • If you're going to feed data to some code, you're way better off fetching data as-is, building the string in code. Commented Oct 8, 2013 at 9:21
  • this function will be called in a stored procedure, I want my programme just call 1 stored procedure instead called other store procedure thousand times :| Commented Oct 8, 2013 at 9:25

1 Answer 1

3

Of course you can:

Using CONCAT for example you can return multiple columns in one row:

SELECT CONCAT(column1,column2,...) FROM ...

And using GROUP_CONCAT you can return multiple rows in one row

SELECT GROUP_CONCAT(column1) FROM... WHERE... GROUP BY column1 

Take a look here http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat and here http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

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

2 Comments

wow thx its help, but why 1st row comes in end ?? the return is "row2, row3,..., rown, row1". I already use order by and its works well, but not with group_concat. any idea ??
oh i see.. order by works in group_concat(), so I must group_concat(column1 order by column2) to make it works. thx sal00m

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.