复杂的mysql查询可能构建子查询

时间:2013-08-06 11:34:30

标签: mysql subquery limit

在我的情况下,我有来自多个表的数据,我想提取单个查询。这是关于足球统计数据。在一分钟内,表格记录了比赛的日期以及其他记录的数据和比赛结果。 问题是我想限制日期的应用程序,而不是匹配的数量,因为在一天有几个游戏。 管理建立一个复杂的查询,显示我的所有数据,但它显示游戏数量而不是日期的结果,所以我不能使用限制,因为在这种情况下吃东西变成更多的游戏而不是日期。 我的问题是,是否可以构建一个对日期有限制的应用程序,同时显示在日期中播放的所有比赛的结果? 这是我现在使用的应用程序的代码:

SELECT
            MAIN.id,
            SECTION.type,
            MAIN.date as date_,
            MAIN.prognosis,
            HOME_TEAM.team_name as home_team,
            GUEST_TEAM.team_name as guest_team,
            FIRST_INDEX.index as f_index,
            SECOND_INDEX.index as s_index,
            THIRD_INDEX.index as t_index,
            DATA.home_result,
            DATA.guest_result,
            DATA.coefficient,
            DATA.success,
            MAIN.total_coefficient,
            MAIN.total_success

        FROM ssdt_matches_main as MAIN

        LEFT JOIN ssdt_section_type as SECTION ON (MAIN.type_id = SECTION.id)
        LEFT JOIN ssdt_matches_data as DATA ON (DATA.matches_main_id = MAIN.id )
        LEFT JOIN ssdt_matches_teams as HOME_TEAM ON (HOME_TEAM.id = DATA.home_team_id )
        LEFT JOIN ssdt_matches_teams as GUEST_TEAM ON (GUEST_TEAM.id = DATA.guest_team_id )
        LEFT JOIN ssdt_matches_index as FIRST_INDEX ON (FIRST_INDEX.id = DATA.first_index_id )
        LEFT JOIN ssdt_matches_index as SECOND_INDEX ON (SECOND_INDEX.id = DATA.second_index_id )
        LEFT JOIN ssdt_matches_index as THIRD_INDEX ON (THIRD_INDEX.id = DATA.third_index_id ) 


        WHERE SECTION.type = 'Risk prognosis'


        ORDER BY MAIN.id DESC

2 个答案:

答案 0 :(得分:1)

您希望将where子句中的日期限制为:

where MAIN.date between date('2012-01-01') and date('2012-12-31');

如果您想从最近10天(匹配)中获取记录,您可以执行以下操作:

select . . .
from . . . join
     (select date
      from ssdt_matches_main md
      group by date
      order by date desc
      limit 10
     ) datel
     on datel.date = MAIN.date

这使用连接来选择日期列表,然后使用连接来进行过滤。

编辑:

您的from子句如下:

    FROM ssdt_matches_main as MAIN

    LEFT JOIN ssdt_section_type as SECTION ON (MAIN.type_id = SECTION.id)
    LEFT JOIN ssdt_matches_data as DATA ON (DATA.matches_main_id = MAIN.id )
    LEFT JOIN ssdt_matches_teams as HOME_TEAM ON (HOME_TEAM.id = DATA.home_team_id )
    LEFT JOIN ssdt_matches_teams as GUEST_TEAM ON (GUEST_TEAM.id = DATA.guest_team_id )
    LEFT JOIN ssdt_matches_index as FIRST_INDEX ON (FIRST_INDEX.id = DATA.first_index_id )
    LEFT JOIN ssdt_matches_index as SECOND_INDEX ON (SECOND_INDEX.id = DATA.second_index_id )
    LEFT JOIN ssdt_matches_index as THIRD_INDEX ON (THIRD_INDEX.id = DATA.third_index_id ) 
    join (select date
          from ssdt_matches_main md
          group by date
          order by date desc
          limit 10
         ) datel
         on datel.date = MAIN.date

答案 1 :(得分:0)

我修复了查询。 没有你的帮助,我不会这样做的。 非常感谢你!

SELECT
            MAIN.id,
            SECTION.type,
            MAIN.date as date_,
            MAIN.prognosis,
            HOME_TEAM.team_name as home_team,
            GUEST_TEAM.team_name as guest_team,
            FIRST_INDEX.index as f_index,
            SECOND_INDEX.index as s_index,
            THIRD_INDEX.index as t_index,
            DATA.home_result,
            DATA.guest_result,
            DATA.coefficient,
            DATA.success,
            MAIN.total_coefficient,
            MAIN.total_success

        FROM ssdt_matches_main as MAIN

LEFT JOIN ssdt_section_type as SECTION ON (MAIN.type_id = SECTION.id)
LEFT JOIN ssdt_matches_data as DATA ON (DATA.matches_main_id = MAIN.id )
LEFT JOIN ssdt_matches_teams as HOME_TEAM ON (HOME_TEAM.id = DATA.home_team_id )
LEFT JOIN ssdt_matches_teams as GUEST_TEAM ON (GUEST_TEAM.id = DATA.guest_team_id )
LEFT JOIN ssdt_matches_index as FIRST_INDEX ON (FIRST_INDEX.id = DATA.first_index_id )
LEFT JOIN ssdt_matches_index as SECOND_INDEX ON (SECOND_INDEX.id = DATA.second_index_id )
LEFT JOIN ssdt_matches_index as THIRD_INDEX ON (THIRD_INDEX.id = DATA.third_index_id ) 
JOIN (SELECT id
      FROM ssdt_matches_main md
      WHERE type_id = 2
      ORDER BY id DESC
      LIMIT 0,5
     ) datel
     ON datel.id = DATA.matches_main_id


        ORDER BY MAIN.id DESC