简体   繁体   English

从联接表中获取最大日期

[英]Get max date from table in join

My first table is a members table: 我的第一个表是成员表:

member_id,  name,   address,    email
1           jon     122 any     jon@test.ca
2           amy     111 gee     amy@test.ca

My second table is test levels accomplished: 我的第二张表是完成的测试级别:

MemberHistoryTable
member_id,  level,  date
1           a       2007
1           b       2008
1           c       2009
1           d       2010
2           a       2007
2           b       2008
2           c       2009
2           d       2010
2           d       2011

I would like to retrive the member information and the most recent date in the history table: 我想检索历史记录表中的成员信息和最新日期:

jon, 122 any, jon@test.ca, 2010
amy, 111 gee, amy@test.ca, 2011

I have tried many different ways of joining the two tables, such as: 我尝试了多种不同的方式来连接两个表,例如:

SELECT
  memberTable.name,
  memberTable.address,
  memberTable.email,
  memberHistoryTable.date
FROM memberTable

LEFT JOIN memberHistoryTable
  ON memberHistoryTable.member_id=memberTable.member_id

But this reproduces multiple rows of the same information, one row for each date... 但这会重现相同信息的多行,每个日期一行。

jon, 122 any, jon@test.ca, 2007
jon, 122 any, jon@test.ca, 2008
jon, 122 any, jon@test.ca, 2009
jon, 122 any, jon@test.ca, 2010
amy, 111 gee, amy@test.ca, 2007  
amy, 111 gee, amy@test.ca, 2008
amy, 111 gee, amy@test.ca, 2009
amy, 111 gee, amy@test.ca, 2010
amy, 111 gee, amy@test.ca, 2011

If I change the join to: 如果我将联接更改为:

LEFT JOIN (SELECT MAX(memberHistoryTable.date) FROM memberHistoryTable) X
  ON x.member_id = memberTable.member_id

I get the rows I want, but the date is NULL... 我得到了想要的行,但日期为NULL ...

    jon, 122 any, jon@test.ca, NULL
    amy, 111 gee, amy@test.ca, NULL

I have tried many variations of joins and none seem to work. 我尝试了多种连接变体,但似乎都没有用。 Either a null value or the system just hangs. 空值或系统只是挂起。

Many hours have been spent searching and I have found many solutions that do not seem to work for me. 搜索已经花费了很多时间,我发现许多解决方案似乎对我不起作用。 Strangely, I need to pull the date from another table for this query, but that one seems to work. 奇怪的是,我需要从另一张表中获取该查询的日期,但是那似乎行得通。

You could use MAX() and a GROUP BY to get your desired result: 您可以使用MAX()GROUP BY获得所需的结果:

SELECT m.name, m.address, m.email, MAX(mh.date) `date` FROM memberTable m
INNER JOIN memberHistoryTable mh ON mh.member_id = m.member_id 
GROUP BY m.name;

I'm assuming the member_id for amy in your members table should be 2 instead of 1 , since it seems like member_id is an unique value identifying each member in your table. 我假设您的members表中amymember_id应该是2而不是1 ,因为似乎member_id是标识表中每个成员的唯一值。

The above query would yield something like: 上面的查询将产生如下内容:

+------+---------+-------------+--------------+
| name | address | email       | date         |
+------+---------+-------------+--------------+
| amy  | 111 gee | amy@test.ca | 2011         |
| jon  | 122 any | jon@test.ca | 2010         |
+------+---------+-------------+--------------+

I also like to point out that date is a keyword in mysql and even if it may be used as an unquoted identifier I would consider naming my column to something else. 我还想指出, date是mysql中的关键字,即使它可以用作未引用的标识符,我也会考虑将我的列命名为其他名称。

I would also suggest that you group by member_id instead of name , since name isn't guaranteed to be unique as I understand it: 我还建议您按member_id而不是name分组,因为按照我的理解,不能保证name是唯一的:

SELECT m.member_id, m.name, m.address, m.email, MAX(mh.date) `date` FROM memberTable m
INNER JOIN memberHistoryTable mh ON mh.member_id = m.member_id 
GROUP BY m.member_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM