2013-08-06 86 views
0

在我的情況下,我有來自多個表的數據,我想提取一個查詢。這是關於足球統計。在一分鐘內記錄比賽日期和其他記錄的數據和比賽結果。 問題是我想限制日期的應用程序,而不是匹配的次數,因爲在一天中有幾場比賽。 管理建立一個複雜的查詢,顯示我的所有數據,但它顯示結果的遊戲數量而不是日期,所以我不能使用限制,因爲在這種情況下吃更多的遊戲,而不是日期。 我的問題是,是否可以構建一個應用程序,該應用程序對日期有限制,同時顯示日期中所有匹配的結果? 這裏是我現在使用的應用程序的代碼:複雜的mysql查詢可能會構建子查詢

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 

回答

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 
+0

我想限制日期數量的數據,而不是日期從 - 到。類似於LIMIT 0,50 – user2656529

+0

@ user2656529。 。 。如果你想限制給定數量的日期,那麼第二個版本應該工作。 –

+0

我想我理解我提出的解決方案的邏輯。我會嘗試返回結果。我不明白的是插入此JOIN的位置以及其他位置保留LEFT ION的位置。這是一個有點困惑的地方......我還沒有完成子查詢 – user2656529

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