我对 SQL 还很陌生,并且正在努力处理查询(使用 Access、FWIW)。我在 Google 上搜索过 StackOverflow,但还没有看到这种确切的场景。 (这也可能是因为我不知道正确的搜索词。)
我有两个非常简单的表,其中包含相似的数据。
table1: state, lname, fname, network
table2: state, lname, fname, network
我想要的是找到两个表中匹配的每个人/状态组合以及该人所在的每个表中的网络:
state, lname, fname, t1.network, t2.network.
该人可能位于每个表中的多个网络中。我想查看此人所属的每个网络(来自两个表)。
我首先使用 JOIN ,如下所示:
SELECT t1.state, t1.lname, t1.fname, t1.network, t2.network
FROM t1 INNER JOIN t2
ON t1.fname=t2.fname AND t1.lname=t2.lname AND t1.state=t2.state
GROUP BY t1.state, t1.lname, t1.fname, t1.network, t2.network
我很快发现我得到了笛卡尔积。因此,如果“NY,Smith,John”位于 t1 中的两个网络和 t2 中的三个网络中,我会得到如下结果:
NY, Smith, John, NetworkA, NetworkB
NY, Smith, John, NetworkA, NetworkA
NY, Smith, John, NetworkB, NetworkA
NY, Smith, John, NetworkB, NetworkB
NY, Smith, John, NetworkA, NetworkC
NY, Smith, John, NetworkB, NetworkC
我真正想看到的只是:
NY, Smith, John, NetworkA, NetworkA
NY, Smith, John, NetworkB, NetworkB
NY, Smith, John, NULL, NetworkC
谁能给我一些关于如何进行的建议或为我指明正确的方向?