douping1825 2015-06-06 17:08
浏览 53
已采纳

mysql与LIMIT LEFT JOIN

I have 2 tables:

games:

g_id | country | team_1 | team_2
--------------------------------
  1  | England | Bayern | Chelsea
  2  | England | Bayern | Liverp
  3  | England | Bayern | Ajax

statistic:

s_id | s_time | s_name   | g_id
-----------------------------
 1   |   4    | Alen A.  |  1
 2   |   7    | Dagn S.  |  1
 3   |   11   | Eden D.  |  1
 4   |   22   | Aren A.  |  1
 5   |   8    | Falen B. |  2
 6   |   66   | Poker G. |  2
 7   |   76   | Nuker S. |  2
 8   |   87   | Eben Y.  |  2
 9   |   18   | Falen B. |  3
 10  |   19   | Aalen F. |  3
 11  |   33   | Gased G. |  3
 12  |   44   | Halen B. |  3

And i'm trying to get data from 2 tables with left join where limit

here a query:

SELECT * 
FROM games
LEFT JOIN statistic
ON games.g_id = statistic.g_id
WHERE games.team1 = 'Bayern'
LIMIT 2

result is:

g_id | country | team_1 | team_2 | s_id | s_time | s_name   | g_id
------------------------------------------------------------------
 1   | England | Bayern | Chelsea|  1   |   4    | Alen A.  |  1
 1   | England | Bayern | Chelsea|  2   |   7    | Dags S.  |  1

i need all data from statistics with limit 2 from table "games"! here example what i need:

g_id | country | team_1 | team_2 | s_id | s_time | s_name   | g_id
------------------------------------------------------------------
 1   | England | Bayern | Chelsea|  1   |   4    | Alen A.  |  1
 1   | England | Bayern | Chelsea|  2   |   7    | Dags S.  |  1
 1   | England | Bayern | Chelsea|  3   |   11   | Eden D.  |  1
 1   | England | Bayern | Chelsea|  4   |   22   | Aren A.  |  1
 2   | England | Bayern | Liverp |  5   |   8    | Falen B. |  2
 2   | England | Bayern | Liverp |  6   |   66   | Dags S.  |  2
 2   | England | Bayern | Liverp |  7   |   76   | Alen A.  |  2
 2   | England | Bayern | Liverp |  8   |   87   | Dags S.  |  2

What query i need?

  • 写回答

1条回答 默认 最新

  • dougou8552 2015-06-06 17:10
    关注

    You can try this:

    SELECT * 
    FROM (SELECT * FROM games WHERE team1 = 'Bayern' ORDER BY g_id LIMIT 2) AS g
    LEFT JOIN statistic AS s
    ON g.g_id = s.g_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3