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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊