我有两个表,想按最新日期查询数据过滤器,尝试使用函数max但不知道如何将它应用于两个表
+-----------+-------------+-------------------+------+
| member id | member name | internet provider | date |
+-----------+-------------+-------------------+------+
| 1 | Sam | c | 2014 |
+-----------+-------------+-------------------+------+
| 2 | johnny | a | 2016 |
+-----------+-------------+-------------------+------+
+-----------+-------------+--+-----------+-------------------+------+
| table A | | | table B | | |
+-----------+-------------+--+-----------+-------------------+------+
| member id | member name | | member id | internet provider | date |
+-----------+-------------+--+-----------+-------------------+------+
| 1 | Sam | | 1 | a | 2012 |
+-----------+-------------+--+-----------+-------------------+------+
| 2 | Johnny | | 1 | b | 2013 |
+-----------+-------------+--+-----------+-------------------+------+
| 3 | Amanda | | 1 | c | 2014 |
+-----------+-------------+--+-----------+-------------------+------+
| | | | 2 | c | 2012 |
+-----------+-------------+--+-----------+-------------------+------+
| | | | 2 | b | 2013 |
+-----------+-------------+--+-----------+-------------------+------+
| | | | 2 | a | 2016 |
+-----------+-------------+--+-----------+-------------------+------+
答案 0 :(得分:1)
查看“聚合函数”。基本上,您将以首选方式加入表格,然后使用group by
。有点像:
select member_name, max(date)
from tableA, tableB
where tableA.member_id = tableB.member_id
group by member_name
如何处理“关系”。有一点需要注意:如果您还想要互联网提供商,您必须弄清楚如何确定它。让我们说成员Sam的最近日期是2014年。如果有两个不同的互联网服务提供商同一个最近的日期怎么办?您可以尝试只将Internet提供商添加到您的查询中,但数据库在处理这种情况方面有所不同。 (您没有说明您正在使用哪一个。)您可能必须使用子查询来查找最大值。