如何在 MySQL 中仅在一个查询中加入两个海关查询和两个联接

How to joint Two customs Queries with two Joins in only One Query in MySQL

查询分别完美运行:

SELECT asf.surface_name, am.*
FROM atp_matchs_to_surfaces m2s
LEFT JOIN atp_surfaces asf ON m2s.surfaces_id = asf.surfaces_id
LEFT JOIN atp_matchs am ON am.matchs_id = m2s.matchs_id;

SELECT att.tournament_type_name, am.*
FROM atp_matchs_to_tournament_type m2s
LEFT JOIN atp_tournament_type att ON m2s.tournament_type_id = att.tournament_type_id
LEFT JOIN atp_matchs am ON am.matchs_id = m2s.matchs_id;

表 'atp_matchs_to_surfaces' 和 'atp_matchs_to_tournament_type' 是这样定义的:

CREATE TABLE IF NOT EXISTS `atp_matchs_to_tournament_type` (
  `tournament_type_id` int(4) NOT NULL,
  `matchs_id` int(6) NOT NULL,
  PRIMARY KEY (`tournament_type_id`,`matchs_id`)

CREATE TABLE IF NOT EXISTS `atp_matchs_to_surfaces` (
  `surfaces_id` int(4) NOT NULL,
  `matchs_id` int(6) NOT NULL,
  PRIMARY KEY (`surfaces_id`,`matchs_id`)

以及包含所有数据的其他表:

CREATE TABLE IF NOT EXISTS `atp_matchs` (
  `matchs_id` int(7) NOT NULL AUTO_INCREMENT,
  `tournament_name` varchar(36) NOT NULL,
  `tournament_year` year NOT NULL,-- DEFAULT '0000',
  `tournament_country` varchar(26) NOT NULL,
  `match_datetime` datetime NOT NULL,-- DEFAULT '0000-00-00 00:00:00',
  `match_link` varchar(85) NOT NULL,
  `prize_money` int(12) NOT NULL,
  `round` varchar(8) NOT NULL,-- DEFAULT '1R',
  `sets` varchar(34) NOT NULL,-- DEFAULT '0-0',
  `result` varchar(4) NOT NULL,-- DEFAULT '0-0',
  `p1_odd` decimal(4,2) NOT NULL,-- DEFAULT '0.00',
  `p2_odd` decimal(4,2) NOT NULL,-- DEFAULT '0.00',
  PRIMARY KEY (`matchs_id`)

CREATE TABLE IF NOT EXISTS `atp_surfaces` (
  `surfaces_id` int(4) NOT NULL AUTO_INCREMENT,
  `surface_name` varchar(24) NOT NULL,
  PRIMARY KEY (`surfaces_id`)

CREATE TABLE IF NOT EXISTS `atp_tournament_type` (
  `tournament_type_id` int(4) NOT NULL AUTO_INCREMENT,
  `tournament_type_name` varchar(22) NOT NULL,
  PRIMARY KEY (`tournament_type_id`)

我想在同一个查询中查询所有的比赛记录和地名+赛事类型。很明显?我希望...

我尝试使用 SubQueries 来实现它:http://www.w3resource.com/mysql/subqueries/ and How can an SQL query return data from multiple tables 但我做不到。

LEFT JOIN 关键字 returns 从左边开始的所有行 table (table1),匹配行在右边 table (table2).

SELECT asf.surface_name, 上午* 从 atp_matchs_to_surfaces 平方米 左连接 (SELECT att.tournament_type, am.* FROM atp_matchs_to_tournament_type m2s) 作为......

好的,这是您当前的架构。如您所见,一场比赛可以在多个场地上进行,一场比赛可以在多种锦标赛类型中进行。

如果这个架构没问题,你可以用这个查询得到你的结果:

SELECT am.*, asu.surface_name, att.tournament_type_name
FROM atp_matchs AS am
LEFT JOIN atp_matchs_to_surfaces AS m2s ON m2s.matchs_id = am.matchs_id
LEFT JOIN atp_surfaces AS asu ON asu.surfaces_id = m2s.surfaces_id
LEFT JOIN atp_matchs_to_tournament_type AS m2t ON m2t.matchs_id = am.matchs_id
LEFT JOIN atp_tournament_type AS att ON att.tournament_type_id = m2t.tournament_type_id

但是,如果一场比赛只能在一个场地上进行并且只能在一种比赛类型中进行,我会将您的架构更改为:

表 atp_matchs_to_surfaces 和 atp_matchs_to_tournament_type 已删除,字段 surfaces_id 和 tournament_type_id 移至 atp_matchs table。您现在的查询是:

SELECT am.*, asu.surface_name, att.tournament_type_name
FROM atp_matchs AS am
LEFT JOIN atp_surfaces AS asu ON asu.surfaces_id = am.surfaces_id
LEFT JOIN atp_tournament_type AS att ON att.tournament_type_id = am.tournament_type_id
SELECT asf.surface_name, am.*
FROM atp_matchs_to_surfaces m2s
LEFT JOIN atp_surfaces asf ON m2s.surfaces_id = asf.surfaces_id
LEFT JOIN atp_matchs am ON am.matchs_id = m2s.matchs_id
LEFT JOIN(
SELECT att.tournament_type, am.*
FROM atp_matchs_to_tournament_type m2s
LEFT JOIN atp_tournament_type att AS Q1 ON m2s.surfaces_id = att.surfaces_id
LEFT JOIN atp_matchs am AS Q2 ON am.matchs_id = m2s.matchs_id);

我添加了一些 "AS" 因为我有错误:每个派生的 table 必须有自己的别名。我有点迷路了!