猿问

LIMIT LEFT 连接到多行中最后更新的行

这是我的代码,我试图加入最新的团队数据,而不是每条数据。我试过只使用限制 1 但没有返回任何东西


ORDER BY 更新的 DESC LIMIT 1


这不起作用


有任何想法吗?


$sql = "SELECT 

        events.id, events.time,events.status, events.home_team,events.away_team,events.league,

        ht.id as home_id,ht.name as home_name,at.name as away_name,

        statistics.home_goals,statistics.away_goals,statistics.time as game_time,

        leagues.id as league_id,leagues.name as league_name,leagues.type as league_type,

        country.name as country_name,country.logo, 

        hts.home_scored, ats.away_scored, 

        hts.home_conceeded,ats.away_conceeded,

        hts.home_win,ats.away_win,

        hts.home_15,ats.away_15,

        hts.home_25,ats.away_25,

        hts.home_btts, ats.away_btts, 

        hts.home_fts, ats.away_fts, 

        hts.home_cs, ats.away_cs, 

        hts.home_corners_for, ats.away_corners_for, 

        hts.home_corners_against, ats.away_corners_against, 

        hts.home_cards, ats.away_cards

        FROM events 

        LEFT JOIN   teams ht

            ON ht.id = events.home_team

        LEFT JOIN   teams at

            ON at.id = events.away_team

        LEFT JOIN leagues

            ON leagues.id = events.league

        LEFT JOIN country 

            ON country.id=leagues.country

        LEFT JOIN ( SELECT team,home_scored,home_conceeded,home_win,home_15,home_25,home_btts,home_fts,home_cs,home_corners_for,home_corners_against,home_cards  FROM team_quick_stats ORDER BY updated DESC)  hts 

            ON ht.id=hts.team

        LEFT JOIN ( SELECT team,away_scored,away_conceeded,away_win,away_15,away_25,away_btts,away_fts,away_cs,away_corners_for,away_corners_against,away_cards  FROM team_quick_stats ORDER BY updated DESC)  ats

            ON at.id=ats.team

        LEFT JOIN   statistics 

            ON statistics.event_id=events.id

        WHERE (events.time BETWEEN $start AND $end) ORDER BY country.list_order, leagues.country ASC , leagues.id ASC, events.time ASC, home_name ASC";


慕码人8056858
浏览 91回答 1
1回答

函数式编程

这是一种方法。替换LEFT JOIN (SELECT team... etc....) ats为...LEFTJOIN ( SELECT x.team       , x.etc...     FROM team_quick_stats x    JOIN        ( SELECT team              , MAX(updated) updated            FROM team_quick_stats           GROUP               BY team       ) y      ON y.team = x.team     AND y.updated = x.updated  ) ats...
随时随地看视频慕课网APP
我要回答