Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何限制内部联接?_Mysql_Sql_Join - Fatal编程技术网

Mysql 如何限制内部联接?

Mysql 如何限制内部联接?,mysql,sql,join,Mysql,Sql,Join,嗨,有没有办法限制我的内心联结 所以我可以限制输出只返回最后5个匹配ID SELECT t.id , ff.match_id , ff.time FROM football_teams t JOIN ( SELECT match_id , hometeam_id , time FROM football_fixtures ORDER BY time

嗨,有没有办法限制我的内心联结

所以我可以限制输出只返回最后5个匹配ID

SELECT t.id
     , ff.match_id
     , ff.time
  FROM football_teams t
  JOIN 
     ( SELECT match_id
            , hometeam_id
            , time
         FROM football_fixtures 
        ORDER 
           BY time DESC 
        LIMIT 5
     ) ff
    ON ff.hometeam_id = t.id
 WHERE t.id IN ( ".$team_id_string." ) 
 ORDER 
    BY t.id
我观察返回一个数组

像这样的

数组([football_team.id]=数组(match_id[1],match_id[2],match_id[3],match_id[4],match_id[5],)


Matt

根据评论,我假设
football\u fixtures
表中的5条最新记录与查询中的其他条件不匹配,因此-不返回任何内容。我建议在外部查询中设置限制,以确保选择了5条记录:

SELECT s.* FROM 
    (SELECT 
            football_teams.id,
            ff.match_id,
            ff.time
            FROM football_teams 
    INNER JOIN football_fixtures ff
     ON hometeam_id=football_teams.id
    WHERE football_teams.id IN ( ".$team_id_string." )
    ORDER BY ff.time
    LIMIT 5) s
ORDER BY s.id

我建议您填写这张表格,以便查询。我删除了连接,因为在这个特定查询中使用它没有意义

SELECT t.match_id, t.hometeam_id as id, t.time FROM (   
    SELECT match_id, hometeam_id, time
    FROM football_fixtures  
    WHERE hometeam_id IN ( ".$team_id_string." ) 
    ORDER BY time DESC 
    LIMIT 5
) as t
ORDER BY id

你的查询是如何工作的?你的查询似乎对你所解释的是正确的。嘿,它什么也没返回,但是如果我把限制放在外面,它会返回所有的东西,如果你喜欢,考虑下面这个简单的两步过程:1。如果您还没有这样做,请提供适当的CREATE和INSERT语句(和/或SQLFIDLE),以便我们可以更轻松地复制问题。2.如果您尚未这样做,请提供与步骤1中提供的信息相对应的所需结果集。这意味着,对于足球赛程中的前5条记录,足球队中没有任何记录。这对我尝试实现的目标不起作用。我最多可以有20个团队id,这将把它们限制在5个,如果有意义的话,每个团队id返回1个匹配id:然后试着解释你想要实现什么MySql知道的唯一数组是JSON数组。因此,计划是1)模拟子查询中的行数()2)按行数将5行数据透视为cols 3)JSON_数组(col1,…col5)