Monty
Monty

Reputation: 1322

Show a MYSQL variable in the result table

How do you show the string value of a user Defined MYSQL variable in the printed table?

Example:

This...

SELECT CONCAT_WS(  ' ', o.FirstName, o.LastName ) AS FirstLast,
`Company` 
FROM Orders o
    

Gives me this..

|---------------|----------|    
|   FirstLast   | Company  |
|---------------|----------|
| First1 Last1  | Company1 |
| First2 Last2  | Company2 |
| First3 Last3  | Company3 |
| First4 Last4  | Company4 |
|---------------|----------|    

But this...

SELECT @firstlast = CONCAT_WS(  ' ', o.FirstName, o.LastName ),
`Company` 
FROM Orders o

Gives me this...

|-------------------------------------------------------------|----------|
|   @firstlast = CONCAT_WS(  ' ', o.FirstName, o.LastName )   | Company  |
|-------------------------------------------------------------|----------|
|                                                       NULL  | Company1 |
|                                                       NULL  | Company2 |
|                                                       NULL  | Company3 |
|                                                       NULL  | Company4 |
|-------------------------------------------------------------|----------|

Obviously this is incorrect, but I don't know what is.

How would I 'print' or display the value? I'm not even sure if I made the variable correct!

Any direction or pointers would be great.

Upvotes: 2

Views: 211

Answers (1)

Mark Byers
Mark Byers

Reputation: 838126

The = operator is for comparison. Since @firstlast is NULL the result of the comparison is NULL.

The assignment operator is :=.

@firstlast := CONCAT_WS(' ', o.FirstName, o.LastName) 

Of course, in your specific example there is no advantage in using a variable.

Upvotes: 4

Related Questions