Mysql 组合两个SQL结果以在具有不同列名的行中形成一个结果集
因为我需要组合两个SQL结果,在结果集中形成一行 我尝试使用Mysql 组合两个SQL结果以在具有不同列名的行中形成一个结果集,mysql,sql,join,union,Mysql,Sql,Join,Union,因为我需要组合两个SQL结果,在结果集中形成一行 我尝试使用联合函数。但结果集有两行,其中opont.*列完全离开opanter.*列。但它应该在结果集中同时包含这两个列。下面是一段代码 SELECT DISTINCT `team_member`.`Team_ID` AS oponent_Team_ID, `team`.`Founder_ID` AS oponent_Founder_ID, `team`.`Team_Logo` AS oponent_Team_Logo, `tea
联合
函数。但结果集有两行,其中opont.*
列完全离开opanter.*
列。但它应该在结果集中同时包含这两个列。下面是一段代码
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)
预期结果:
+-----------------+--------------------+-------------------+--------------------+-------------------+-------------------+-------------------+-------------------+-------------------+----------------+
| 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)
需要获得结果集中一行与不同列名组合的结果。让我知道除此之外还有其他功能吗。您可以在
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
@尼桑斯ॐ, 在“字段列表”中的未知列“反对者团队1\u id”中选择此处获取错误作为。
@Nishanthॐ, chk now-当您执行union时,就不需要为第二个查询定义列名,所以这就是为什么它最终给出了错误!