NorCode
NorCode

Reputation: 689

how to combine two select reslult as column

I want to combine two select statement result as column structure not row. But i am not get my result. Please tell me how to do this type of result in mysql.

My code:

SELECT concat(ClassName,'/',Sub1) as PI 
from timetable  
where P1='AKS' and day='Mon' 

union all 
select concat(ClassName,'/',Sub2) as PII 
from timetable  
where P2='AKS' and day='Mon';

and its output in workbench

enter image description here

In this output Result of union all is in PI column but i want PI,PII as column not with in row .

please tell me how

my table: enter image description here

Upvotes: 0

Views: 157

Answers (1)

fancyPants
fancyPants

Reputation: 51888

No offense, but that's a classical case of not having understood the concepts of relational databases. It's not like Excel or something like that, where you simply push something in another column.

There seems to be no connection between the data "PI" and "PII", so it makes no sense to have it "side by side". I also wonder, why you want to have it like that. I recommend you do it like this:

SELECT 'PI' AS where_the_data_is_coming_from, concat(ClassName,'/',Sub1) as P_columns
from timetable  
where P1='AKS' and day='Mon' 

union all 

select 'PII', concat(ClassName,'/',Sub2) 
from timetable  
where P2='AKS' and day='Mon';

Include a string which will tell you in the result, if the data belongs to "PI" or "PII". Then work with the data from there.

Upvotes: 1

Related Questions