SQL Server:使用另一个表作为“查找”
我从表中选择一些数据,其中一列是定义一个人的值。该值对该人来说是唯一的,但可以在表中多次出现。
我没有关于数据库的任何文档 - 人员的详细信息(姓名等)保存在另一个表中,但它有一个不同 ID 列,并且不引用第一个表中的 ID。
有一个第三表(请听我说!),其中包含第一个表中的 ID 和第二个表中的 ID。因此,我尝试使用它在两个 ID 之间创建“链接”,这样我就可以从第二个表中获取所需的人员数据。第三个表包含的记录比其他两个表多得多。
我尝试了各种连接,但返回的数据始终包含重复项和不正确的 ID。我将尝试举一个例子:
表1(我正在查询的表):
AgentID | Date | Time |
---|---|---|
9000 | 1/1/2022 | 12:00:00 |
9000 | 1/2/2022 | 15:00:00 |
9001 | 1/1/ 2022 | 13:00:00 |
9001 | 2022 年 1 月 2 日 | 17:00:00 |
表 2(用户信息):
UserID | 名称 |
---|---|
1000 | Fred Bloggs |
1001 | John Smith |
表 3(“链接”):
UserID | AgentID | 其他数据 |
---|---|---|
1000 | 9000 | … |
1001 | 9001 | |
1001 | 9001 | |
1000 | 9000 | |
1001 | 9001 | |
1000 | 9000 | 等 |
两者都不是UserID
或 AgentID
是各自表中的 PK 或 FK(不要问 - 我们没有设计这个......)
有没有办法做我的事情我想做什么?似乎应该有,但我碰壁了。
我尝试过的示例:
SELECT
table2.name, table1.date,table1.time
FROM
table2
LEFT JOIN
table3 ON table3.agentID = table1.agentID
LEFT JOIN
table2 ON table2.UserID = table3.UserID
我也尝试过内部连接和右连接,结果相同。
我需要进行某种“嵌套”连接吗?
I am selecting some data from a table where one of the columns is a value that defines a person. The value is unique to that person but can appear multiple times in the table.
I have no documentation whatsoever on the db - the details of the person (name etc) are held in another table, but that has a different ID column and does not reference the ID in the first table.
There is a third table (stay with me!) that contains both the ID from the first table, and the id from the second table. So I'm trying to use this to create a 'link' between the two IDs, so I can then grab the person data I need from the second table. The third table contains many more records than the other two.
I've tried various joins but the data I get back always contains duplicates, and incorrect IDs. I'll try and give an example:
Table 1 (the table I'm querying):
AgentID | Date | Time |
---|---|---|
9000 | 1/1/2022 | 12:00:00 |
9000 | 1/2/2022 | 15:00:00 |
9001 | 1/1/2022 | 13:00:00 |
9001 | 1/2/2022 | 17:00:00 |
Table 2 (the user info):
UserID | Name |
---|---|
1000 | Fred Bloggs |
1001 | John Smith |
Table 3 (the 'link'):
UserID | AgentID | Other Data |
---|---|---|
1000 | 9000 | … |
1001 | 9001 | |
1001 | 9001 | |
1000 | 9000 | |
1001 | 9001 | |
1000 | 9000 | etc |
Neither UserID
or AgentID
are PKs or FKs in the respective tables (don't ask - we didn't design this....)
Is there a way to do what I'm trying to do? It seems there should be but I've hit a brick wall.
Example of what I've tried:
SELECT
table2.name, table1.date,table1.time
FROM
table2
LEFT JOIN
table3 ON table3.agentID = table1.agentID
LEFT JOIN
table2 ON table2.UserID = table3.UserID
I've tried inner and right joins as well, same results.
Do I need to be doing some sort of 'nested' join?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用子查询,首先您应该将table1与table3(链接)的不同值连接起来,第二您应该将table2(UserInfo)与表连接起来关联。在查询之后,
您应该根据表的名称更改查询。
you should use Subquery, first you should join table1 with distinct value of table3(Link), second you should join table2 (UserInfo)with table link. following query
you should change the query per the names of your tables.