MySQL - 将两列拆分为两个不同的行

时间:2021-06-16 21:46:24

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

只是一个注释我知道如何连续使用concat函数连续使用我不知道如何使用两行

3 个解决方案

#1


9  

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

您可以使用UNION ALL和ORDER BY Totalwatch DESC来获取根据Totalwatch订购的结果。

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

#2


2  

Simply use UNION ALL:

只需使用UNION ALL:

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

#3


2  

Try this bro.. :)

试试这个兄弟.. :)

SELECT HomeTeam,Totalwatch
FROM   YourTable

UNION ALL

SELECT AwayTeam,Totalwatch
FROM   YourTable

#1


9  

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

您可以使用UNION ALL和ORDER BY Totalwatch DESC来获取根据Totalwatch订购的结果。

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

#2


2  

Simply use UNION ALL:

只需使用UNION ALL:

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

#3


2  

Try this bro.. :)

试试这个兄弟.. :)

SELECT HomeTeam,Totalwatch
FROM   YourTable

UNION ALL

SELECT AwayTeam,Totalwatch
FROM   YourTable