Home > Blockchain >  how can i change structure of the way i retrieve the data in mysql
how can i change structure of the way i retrieve the data in mysql

Time:11-24

i have this data set

Family Members type of service 1 Type of Service 2 Type of service 3
Family 1 Member 1 Cash Education Health
Family 1 Member 2 Education work cash
Family 2 Member 1 health food cash

i want a query that makes the output like this structure:

family Member Type of service
Family 1 Member 1 Cash
Family 1 Member 1 Education
family 1 Member 1 health
Family 1 Member 2 Education
Family 1 Member 2 work
Family 1 Member 2 cash

CodePudding user response:

We can use a union approach:

SELECT Family, Members, Service1 AS Service FROM yourTable
UNION ALL
SELECT Family, Members, Service2 FROM yourTable
UNION ALL
SELECT Family, Members, Service3 FROM yourTable
ORDER BY Family, Members, Service;
  • Related