0

I'm new to Mysql. In one of my mysql tables, I have a row that two columns involved and I need to list user as multiple rows, I'm wondering if it is possible.

For instance, in mysql table1, there is only one user1 as below;

 id      username       value1       value2
 ===========================================
 1       user1          abc           def

I want to group user1 by pass1 and pass2 with an additional row as below;

 id      username       allvalues     value
 =============================================
 1       user1          value1          abc
 2       user1          value2          def

id number is not important at that point as long as it is uniqe.

Thanks in advance.

1
  • Consider amending the design of table1 so that it looks more like table2 to begin with. Note that id='2' is rather meaningless in the present context. Commented Aug 27, 2018 at 9:40

2 Answers 2

2

you could use a UNION

select  username, 'value1' allvalues , value1 
from my_table  
union  
select  username, 'value2' , value2
from my_table  
Sign up to request clarification or add additional context in comments.

Comments

1

Try this with UNION

select username,'value1' as allvalues, value1 as value from tablename
union
select username,'value2' as allvalues, value2 as value from tablename

1 Comment

thanks. I did not know union is for this purpose. However, id numbers end up with the same as '1' that i need to increase id number +1 for every row.

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.