如何使用表之间的连接? [英] How to use connections between tables?

查看:122
本文介绍了如何使用表之间的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的表帐户 id ,表会话 with account_id 。字段已连接。那么它的可用性是什么?



我尝试过:



要谷歌并在此处找到它。

解决方案

这些列用于将表及其实体链接在一起。给你一个特定的场景,只需考虑以下内容,

账户
id |名字| ...
1 | ABCD | ...
2 | EFGH | ...

会话
id | account_id | ...
1 | 1 | ...
2 | 43 | ...
3 | 3 | ...
4 | 1 | ...
5 | 2 | ...



例如,在您想要显示从两个表中链接到特定帐户的所有会话的状态中,您将使用来自帐户的ID和Account_Id从Sessions表到 JOIN 他们在一起。在SQL JOIN中,您可以根据以下标准指定 JOIN 的记录 -



因此,得到这样的东西,

 Account.id | Sessions.id |名字| ... 
1 | 1 | ABCD | ...
1 | 4 | ABCD | ...



那么进一步的东西是你自己使用数据的逻辑,怎么样?这一切都取决于你,基本上这是在已经规范化的表上完成的。您从表中删除了一些常见的异常,并删除了一些数据冗余。该步骤有时会导致超过1,2或甚至3个表。因此,获取数据需要您加入表格。这些列 - account.id sessions.account_id - 充当表格的轴心点 JOIN ed at。



为了更好地解释如何在SQL中加入表格,请阅读以下文章,

SQL联接的可见表示 [ ^ ]

SQL Server中的连接类型 [ ^ ]

数据库规范化 - 维基百科 [ ^


For example I have table accounts with id, and table sessions with account_id. The fields are connected. So what is the usability of it?

What I have tried:

To google and to find it here.

解决方案

Those columns are used to link together the tables and their entities. Giving you a specific scenario, just think of the following content,

Account
id | name | ...
1  | ABCD | ...
2  | EFGH | ...

Sessions
id | account_id | ...
1  | 1          | ...
2  | 43         | ...
3  | 3          | ...
4  | 1          | ...
5  | 2          | ...


For example, in the states where you want to show all the sessions linked to a specific account from both the tables, you would use the ID from Account and Account_Id from Sessions table to "JOIN" them together. In SQL JOIN, you specify which records to JOIN based on which criteria — the columns.

Thus, getting something like this,

Account.id | Sessions.id | name | ...
1          | 1           | ABCD | ...
1          | 4           | ABCD | ...


Then further stuff is your own logic to use the data, how? That is all upto you, basically this is done on the tables that have been normalized. You remove a few common anomalies from the tables, and you remove some data redundancies. That step results in more than 1, 2 or even 3 tables sometimes. Thus getting the data back requires you to JOIN the tables. These columns — account.id, sessions.account_id — act as the pivot points for your tables to be JOINed at.

For a better explanation of how to JOIN the tables in SQL, please read the following articles,
Visual Representation of SQL Joins[^]
Types of Join in SQL Server[^]
Database normalization - Wikipedia[^]


这篇关于如何使用表之间的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆