Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 既然我可以用更简单的方式连接两个列,为什么还要连接它们呢?_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 既然我可以用更简单的方式连接两个列,为什么还要连接它们呢?

Sql 既然我可以用更简单的方式连接两个列,为什么还要连接它们呢?,sql,sql-server,tsql,Sql,Sql Server,Tsql,假设我们有两张桌子,其中一张桌子上有我们的客户,另一张桌子上有我们的订单,两张桌子上都有客户id 因此,我想获得特定客户的订单,在这里,我有两种方法: 我可以说Select*from orders,其中id='1' 我可以说Select*from customers在id=customers.id上加入订单,其中id='1' 所以我要问,为什么我要使用第二种方法,因为它的书写时间更长,或者还有其他用途吗?如果您有客户id,那么只需执行以下操作: select o.* from orders o

假设我们有两张桌子,其中一张桌子上有我们的客户,另一张桌子上有我们的订单,两张桌子上都有客户id

因此,我想获得特定客户的订单,在这里,我有两种方法:

我可以说Select*from orders,其中id='1' 我可以说Select*from customers在id=customers.id上加入订单,其中id='1'
所以我要问,为什么我要使用第二种方法,因为它的书写时间更长,或者还有其他用途吗?

如果您有客户id,那么只需执行以下操作:

select o.*
from orders o
where o.customer_id = 1;
如果要使用其他信息,只需将表连接在一起,例如:

select o.*
from orders o join
     customers c
     on o.customer_id = c.customer_id
where c.email = @email;

第一种方法不会向您提供任何客户详细信息,也不会让您根据客户表中的任何值进行筛选,而不是IDA。您这样做的另一个原因是获取客户名称和/或发货信息。通常,客户ID对大多数人来说并不意味着什么。但是,如果您不想从客户那里获得信息,则无需将这两个表连接起来。Orders表中的客户ID被称为外键,希望能够在这两个表以及最有可能的其他表之间保持引用完整性。谢谢。我得到了它。