SQL连接和分组问题
首先要说的是,我正在使用 mySQL,并且访问数据库的唯一方法是通过 phpMyAdmin,因为这是针对网站的。
首先,我有一个像这样的用户表(表1):
user_id | name
--------------
1 | Mike
2 | Johny
3 | Bob
还有另一个关于他们的赛季结果的表(表2):
user_id | season | result
-------------------------
1 | 2009 | 10
1 | 2010 | 8
1 | 2011 | 5
2 | 2009 | 7
2 | 2010 | 3
3 | 2009 | 1
现在我想要用户的名字和他上赛季的结果,所以像这样:
user_id | name | season | result
--------------------------------
1 | Mike | 2011 | 5
2 | Johny| 2010 | 3
3 | Bob | 2009 | 1
我最接近的东西就像这样,
SELECT * FROM table1 INNER JOIN table2 ON table1.user_id = table2.user_id GROUP BY id
但我无法提供我想要最新一季的信息,它只是使用随机的一季。 任何人有任何想法如何做到这一点?
此致, 洛克
First to say I'm using mySQL and my only way of accessing database is over phpMyAdmin since this is for a website.
First I have a table of users like this (table1):
user_id | name
--------------
1 | Mike
2 | Johny
3 | Bob
and another table about their season results (table2):
user_id | season | result
-------------------------
1 | 2009 | 10
1 | 2010 | 8
1 | 2011 | 5
2 | 2009 | 7
2 | 2010 | 3
3 | 2009 | 1
And now I want out the user's name and his last season's result, so something like this:
user_id | name | season | result
--------------------------------
1 | Mike | 2011 | 5
2 | Johny| 2010 | 3
3 | Bob | 2009 | 1
The closest I have come to something like this was
SELECT * FROM table1 INNER JOIN table2 ON table1.user_id = table2.user_id GROUP BY id
But I cannot provide that I want the newest season and it just uses a random one.
Anyone has any ideas how to do this?
best regards,
Rok
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过按季节栏排序?
Have you tried to order them by Season column?