首页 > 解决方案 > MS-Access - 根据条件合并两个表之间的数据

问题描述

我正在使用 MS Access,我正在尝试在两个表之间创建一个查询并基于以下条件合并相同的行:

cust_id = cust_id并且 a_date = f_dateprice = paid

并有欲望输出。

我现在的数据:

tblapp
app_id  cust_id    a_date    price  a_memo
------------------------------------------
1         1       10/10/20     20   hello
2         1       11/10/20     10   bye
3         2       12/10/20     30   hi
4         2       12/10/20     30   text
5         2       12/10/20     30   lol
6         2       12/10/20     30   ciao
7         3       14/10/20     25   peace

tblfin
fin_id  cust_id    f_date     paid
----------------------------------
1         1       10/10/20     20
2         1       11/10/20     10
3         1       11/10/20     10
4         2       12/10/20     30
5         3       14/10/20     25

如你看到的,

cust_id = 1on 10/10/20with bill 20and paid20

cust_id = 1on 11/10/20with bill 10and paid10 + 10

cust_id = 2on 12/10/20with bill 30 + 30 + 30 + 30and paid30

cust_id = 3on 14/10/20with bill 25and paid25

Derire 查询输出:

app_id cust_id  a_date   price  a_memo  fin_id  cust_id   f_date  paid
----------------------------------------------------------------------
1         1    10/10/20   20    hello      1       1     10/10/20  20
2         1    11/10/20   10    bye        2       1     11/10/20  10
3         2    12/10/20   30    hi         4       2     12/10/20  30
7         3    14/10/20   25    peace      5       3     14/10/20  25

尝试了以下 sql,但我得到了重复项(例如 cust_id 1 和 2,其中两个表中的数据行不同):

SELECT f.fin_id,
       f.cust_id,
       f.f_date,
       f.paid,
       a.app_id,
       a.cust_id,
       a.a_date,
       a.price,
       a.a_memo
FROM tblfin AS f
LEFT JOIN tblapp AS a ON (f.cust_id=a.cust_id)
AND (f.f_date=a.a_date)
AND (f.paid=a.price);

欢迎使用 MySQL 的解决方案。谢谢你。

标签: sqlms-accessjoin

解决方案


推荐阅读