且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用SQL查询从多个表中获取最早的日期?

更新时间:2023-01-20 12:31:32

要获取B_ID的最小日期,请使用

To get the min date for a B_ID use

select min(c_date) from TableC where b_id = XXXX

获取所有最小日期

select b_id, min(c_date) as MinDate
from TableC group by b_id

然后您可以将该查询加入其他表:

You can then join that query on to other tables:

select *
from TableB
left join (
    select b_id, min(c_date) as MinDate
    from TableC group by b_id
) as minimumDates
    on minimumDates.b_id = TableB.b_id

如果查询语法有问题,请尝试直接针对MS Access运行SQL,而不是通过VB运行SQL.这样可以更轻松地检查哪些内容无效/无效.如果您无法让GROUP BY正常工作,那么就从一个尽可能简单的新查询开始,就像上面的第一个select一样.忘记了您真正想要的列,只需运行一个简单的查询,然后一次添加JOIN s和WHERE以及一列即可.

If you're having trouble with query syntax try to run your SQL against MS Access directly rather than via VB. That'll make it easier to check what does/doesn't work. If you can't get the GROUP BY to work then start with a new query as simple as possible, like with my first select above. Forget which columns you really want, just get a simple query working and then add JOINs and WHERE and columns one at a time.