简体   繁体   中英

MySQL - Split two columns into two different rows

I don't have any idea to create this sorry if it is a silly question.

I have a table two teams and total watch and I will use this information later a different place so my idea concat this two column in one column but two different rows:

HomeTeam      AwayTeam     Totalwatch 
A              B              100
A              C               90
C              A               80
D              B               70
C              E               50

Can I this

Teams          TotalWatch
A                100
B                100
A                 90
C                 90
C                 80
A                 80
D                 70
B                 70
C                 50
E                 50

I have a few columns so they will repeat as well.

Just a note I know how can concat in one-row use with concat function I do not know how can I make with two rows

You can use UNION ALL and an ORDER BY Totalwatch DESC to get the results ordered according to Totalwatch .

SELECT HomeTeam AS Teams, Totalwatch  FROM YourTable
UNION ALL
SELECT AwayTeam, Totalwatch FROM YourTable
ORDER BY Totalwatch DESC;

Simply use UNION ALL :

SELECT * 
FROM(
    SELECT HomeTeam Teams,TotalWatch FROM Your_Table
    UNION ALL
    SELECT AwayTeam,TotalWatch FROM Your_Table
    )D
ORDER BY TotalWatch DESC

Try this bro.. :)

SELECT HomeTeam,Totalwatch
FROM   YourTable

UNION ALL

SELECT AwayTeam,Totalwatch
FROM   YourTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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