[英]MySql case statement returning multiple rows and multiple columns
I have a table 我有桌子
-id --col_1 -- col_2 -- col3 --col_4 -- col5
------------------------------------------------
1 a b c d e
2 a1 b1 c1 d1 e1
3 a2 b2 c2 d2 e2
I need to check if col3 contains a value, if present i need to display col1 and col2 values and remaining values should be empty. 我需要检查col3是否包含一个值,如果存在,我需要显示col1和col2值,其余值应为空。 Another condition if col4 contains value, i need to display only col2 and col5 values 如果col4包含值的另一种情况,我只需要显示col2和col5值
-id --col_1 -- col_2 -- col3 --col_4 -- col5
------------------------------------------------
1 a b c
1 b1 d1 e1
2 a1 b1 c1
2 b1 d1 e1
3 a2 b2 c2
3 b2 d2 e2
I am unaware of which procedure to follow to achieve my requirement. 我不知道要达到我的要求应该遵循哪个程序。 I have used case statement, i have retrieved only one record for two conditions with only one column 我用过case语句,我只检索了一条条件的两个条件的一条记录
SELECT
CASE
WHEN col3 != '' THEN col4
END as a,
CASE
WHEN col4 != '' THEN col2
END as b
FROM table;
Any Help!.. 任何帮助!
If I understand correctly, you just want union all
: 如果我理解正确,则只需要union all
:
select id, col1, col2, col3, NULL as col4, NULL as col5
from table
union all
select id, NULL, col2, NULL, col4, NULL as col5
from table ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.