将两个SQL结果组合在一起以形成一个具有不同列名的结果集

Nɪsʜᴀɴᴛʜ

因为我需要合并两个SQL结果,在结果集中形成一行。

我试图利用union功能。但是结果在结果集中两行,oponent_*opposer_*列完全离开列。但是它应该在结果集中同时包含这两个列。这是下面的代码

SELECT DISTINCT
  `team_member`.`Team_ID` AS oponent_Team_ID,
  `team`.`Founder_ID` AS oponent_Founder_ID,
  `team`.`Team_Logo` AS oponent_Team_Logo,
  `team`.`team_Name` AS oponent_Founder_ID,
  `teams_game_match`.`team_1_id` AS oponent_team_1_id,
  `teams_game_match`.`team_2_id` AS oponent_team_2_id,
  `teams_game_match`.`game_time` AS oponent_game_time,
  `teams_game_match`.`game_date` AS oponent_game_date,
  `teams_game_match`.`game_name` AS oponent_game_name,
  `teams_game_match`.`accept` AS oponent_accept
FROM
  `team_member`
  JOIN `team`
    ON `team_member`.`Team_ID` = `team`.`Team_ID`
  JOIN `teams_game_match`
    ON `teams_game_match`.`team_2_id` = `team`.`Team_ID`
WHERE `teams_game_match`.`team_1_id` = '11'

UNION

SELECT DISTINCT
  `team_member`.`Team_ID` AS opposer_Team_ID,
  `team`.`Founder_ID` AS opposer_Founder_ID,
  `team`.`Team_Logo` AS opposer_Team_Logo,
  `team`.`team_Name` AS opposer_Founder_ID,
  `teams_game_match`.`team_1_id` AS opposer_team_1_id,
  `teams_game_match`.`team_2_id` AS opposer_team_2_id,
  `teams_game_match`.`game_time` AS opposer_game_time,
  `teams_game_match`.`game_date` AS opposer_game_date,
  `teams_game_match`.`game_name` AS opposer_game_name,
  `teams_game_match`.`accept` AS opposer_accept
FROM
  `team_member`
  JOIN `team`
    ON `team_member`.`Team_ID` = `team`.`Team_ID`
  JOIN `teams_game_match`
    ON `teams_game_match`.`team_1_id` = `team`.`Team_ID`
WHERE `teams_game_match`.`team_2_id` = '11'

实际结果:

+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
| oponent_Team_ID | oponent_Founder_ID | oponent_Team_Logo | oponent_Founder_ID | oponent_team_1_id | oponent_team_2_id | oponent_game_time | oponent_game_date | oponent_game_name | oponent_accept |
+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
|               4 |                  1 | 486396439.png     | Nish               |                11 |                 4 | 1:30am            | 2019-06-28        | Battlefield 4     |              0 |
|              13 |                  7 | 557132285.png     | BFM                |                13 |                11 | 1:30am            | 2019-07-12        | FIFA 17           |              1 |
+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
2 rows in set (0.00 sec)

预期结果:

+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
| oponent_Team_ID | oponent_Founder_ID | oponent_Team_Logo | oponent_team_Name  | oponent_team_1_id | oponent_team_2_id | oponent_game_time | oponent_game_date | oponent_game_name | oponent_accept | opposer_Team_ID | opposer_Founder_ID | opposer_Team_Logo | opposer_team_Name  | opposer_team_1_id | opposer_team_2_id | opposer_game_time | opposer_game_date | opposer_game_name | opposer_accept |
+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
|               4 |                  1 | 486396439.png     | Team 1             |                11 |                 4 | 1:30am            | 2019-06-28        | Battlefield 4     |              0 |              13 |                  7 | 557132285.png     | Team 3             |                13 |                11 | 1:30am            | 2019-07-12        | FIFA 17           |              1 |
+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
1 rows in set (0.00 sec)

SQL小提琴演示

要获得包含一行结果并与结果集中的不同列名结合在一起的结果,则是必需的。让我知道除此以外还有其他功能。

法米

您可以尝试使用 case when

演示

select max(case when flag=1 then   oponent_Team_ID end) as oponent_Team_ID,
max(case when flag=0 then   oponent_Team_ID end) as opposer_Team_ID

from
(
SELECT DISTINCT
  `team_member`.`Team_ID` AS oponent_Team_ID,
  `team`.`Founder_ID` AS oponent_Founder_ID,
  `team`.`Team_Logo` AS oponent_Team_Logo,
  `team`.`team_Name` AS oponent_Founder_Name,
  `teams_game_match`.`team_1_id` AS oponent_team_1_id,
  `teams_game_match`.`team_2_id` AS oponent_team_2_id,
  `teams_game_match`.`game_time` AS oponent_game_time,
  `teams_game_match`.`game_date` AS oponent_game_date,
  `teams_game_match`.`game_name` AS oponent_game_name,
  `teams_game_match`.`accept` AS oponent_accept, 1 as flag
FROM
  `team_member`
  JOIN `team`
    ON `team_member`.`Team_ID` = `team`.`Team_ID`
  JOIN `teams_game_match`
    ON `teams_game_match`.`team_2_id` = `team`.`Team_ID`
WHERE `teams_game_match`.`team_1_id` = '11'

UNION

SELECT DISTINCT
  `team_member`.`Team_ID` AS opposer_Team_ID,
  `team`.`Founder_ID` AS opposer_Founder_ID,
  `team`.`Team_Logo` AS opposer_Team_Logo,
  `team`.`team_Name` AS opposer_Founder_ID,
  `teams_game_match`.`team_1_id` AS opposer_team_1_id,
  `teams_game_match`.`team_2_id` AS opposer_team_2_id,
  `teams_game_match`.`game_time` AS opposer_game_time,
  `teams_game_match`.`game_date` AS opposer_game_date,
  `teams_game_match`.`game_name` AS opposer_game_name,
  `teams_game_match`.`accept` AS opposer_accept,0
FROM
  `team_member`
  JOIN `team`
    ON `team_member`.`Team_ID` = `team`.`Team_ID`
  JOIN `teams_game_match`
    ON `teams_game_match`.`team_1_id` = `team`.`Team_ID`
WHERE `teams_game_match`.`team_2_id` = '11'
)A

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将两个具有不同行大小的数据框组合在一起?

将两个查询组合在一起以形成月份计数

将两个具有不同位置的SQL查询组合在一起并返回唯一的行

将两个不同的表组合在一起进行查询

MySQL将两个结果结合在一起?

将两个输入字段的结果与JavaScript结合在一起?

如何将两个不同查询的结果与猫鼬结合在一起?

如何将两个SQL查询与不同的条件组合在一起?

如何将两个具有相同键的数组组合在一起?

合并两个具有Employee不同属性的数组列表以创建一个新的数组列表,将两个数组列表的属性组合在一起

如何将2个Foreach循环与不同的数组结果组合在一起?

将两个或多个线程组合在一起

将两个IQueryable组合在一起不起作用

将两个 -filter_complex 命令组合在一起

Python 以字母开头的行,将两个 for 循环组合在一起?

如何将两个通用队列组合在一起?

如何在Python中将两个具有相同索引的Series组合在一起?

两个具有几乎相同操作的实体是否应该组合在一起?

Oracle:通过联合将两个按查询组合在一起(它们使用聚合函数count())以得到合并结果

将一个元素与不同列的所有其他元素组合在一起 / R

将2个具有不同列数的数据框组合在一起,并用和求和

如何返回将两个函数与两个参数组合在一起的函数

如何使用 plot_grid() 将两个图组合在一起在“cowplot”中具有单个网格背景?在 R 语言中

将具有共同元素的两个列表结合在一起-Scala

如何通过将每个列表中的一个元素组合在一起,将两个不同对象的列表组合为一个列表?

一个将单个结果与多个其他结果结合在一起的数据块

用公共行映射两个不同的数据框并将它们组合在一起

如何在MySQL中将两个不同的表组合在一起并对其进行排序

两个列表整数在python中组合在一起