简体   繁体   English

在mysql中按行与多行选择多列

[英]Select row by multiple columns with multiple rows in mysql

I'm new to Mysql. 我是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. 在我的一个mysql表中,有一行涉及两列,并且我需要将用户列为多行,我想知道是否有可能。

For instance, in mysql table1, there is only one user1 as below; 例如,在mysql table1中,只有一个user1如下:

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

I want to group user1 by pass1 and pass2 with an additional row as below; 我想通过pass1和pass2将user1分组,并附加如下一行;

 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. ID号在那时并不重要,只要它是唯一的即可。

Thanks in advance. 提前致谢。

Try this with UNION 与UNION一起尝试

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

you could use a UNION 你可以使用UNION

select  username, 'value1' allvalues , value1 
from my_table  
union  
select  username, 'value2' , value2
from my_table  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM