【问题标题】:How to joint Two customs Queries with two Joins in only One Query in MySQL如何在 MySQL 中仅在一个查询中连接两个自定义查询和两个连接
【发布时间】:2015-03-04 09:34:40
【问题描述】:

查询分别完美地工作:

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`)

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

我尝试使用子查询来实现这一点:http://www.w3resource.com/mysql/subqueries/How can an SQL query return data from multiple tables,但我无法正常工作。

【问题讨论】:

  • 你应该指出所有涉及的表的列,因为我们看不到你提供的两个表之间的任何连接 atp_matchs_to_tournament_type 和 atp_matchs_to_surfaces
  • 我们开始吧。我按要求添加了其他涉及的表格。谢谢。

标签: mysql sql select subquery left-join


【解决方案1】:

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

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

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 表。您现在的查询是:

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

【讨论】:

  • 感谢您的回答!如果我执行 UNION 或 UNION ALL,则在第二个 SELECT 中定义的“att.tournament_type_name”不会显示在最终结果中。它只显示第一个 SELECT 中的“asf.surface_name”。
  • 结果的列名取自第一个SELECT,因此'att.tournament_type_name'和'asf.surface_name'的混合值
  • 啊,好吧,现在我明白了。所以工会没有做我想做的工作。
  • 抱歉误会,您想将这些值放在结​​果的不同列中吗?换句话说,您是否希望获得比赛列表和相关锦标赛类型以及每个比赛的表面?
  • 我想要 'att.tournament_type_name'+'asf.surface_name'+am.*(所有 atp_matchs 记录)在相同的结果中。用人类的话来说,我想要每场比赛,比赛的记录,表面(可以是硬的,粘土...)和比赛的类型(可以是 ATP1000/ATP500/ATP250...)。我现在更清楚了?呵呵谢谢你的帮助!
【解决方案2】:

LEFT JOIN 关键字返回左表 (table1) 中的所有行,以及右表 (table2) 中的匹配行。

选择 asf.surface_name, am.* FROM atp_matchs_to_surfaces m2s 左连接(选择 att.tournament_type,am.* FROM atp_matchs_to_tournament_type m2s) as......

【讨论】:

  • 对不起@Akash kumar,但我做不到。您能否编写解决方案示例的工作代码或简化的工作代码。谢谢。
  • 如果您按照建议的方式实现了查询,请粘贴查询。
【解决方案3】:
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”,因为我遇到了错误:每个派生表都必须有自己的别名。我有点迷路了!

【讨论】:

    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2014-12-09
    • 2015-10-08
    • 1970-01-01
    相关资源
    最近更新 更多