简体   繁体   English

在 Select 中合并两个表 (SQL Server 2008)

[英]Combine Two Tables in Select (SQL Server 2008)

If I have two tables, like this for example:如果我有两个表,例如这样:

Table 1 (products)表1(产品)

id
name
price
agentid

Table 2 (agent)表2(代理)

userid
name
email

How do I get a result set from products that include the agents name and email, meaning that products.agentid = agent.userid ?如何从包含代理名称和电子邮件的产品中获取结果集,即products.agentid = agent.userid

How do I join for example SELECT WHERE price < 100 ?我如何加入例如SELECT WHERE price < 100

Edited to support price filter编辑以支持价格过滤器

You can use the INNER JOIN clause to join those tables.您可以使用INNER JOIN子句连接这些表。 It is done this way:它是这样完成的:

select p.id, p.name as ProductName, a.userid, a.name as AgentName
from products p
inner join agents a on a.userid = p.agentid
where p.price < 100

Another way to do this is by a WHERE clause:另一种方法是通过WHERE子句:

select p.id, p.name as ProductName, a.userid, a.name as AgentName
from products p, agents a
where a.userid = p.agentid and p.price < 100

Note in the second case you are making a natural product of all rows from both tables and then filtering the result.请注意,在第二种情况下,您正在对两个表中的所有行进行自然乘积,然后过滤结果。 In the first case you are directly filtering the result while joining in the same step.在第一种情况下,您在加入同一步骤时直接过滤结果。 The DBMS will understand your intentions (regardless of the way you choose to solve this) and handle it in the fastest way. DBMS 将了解您的意图(无论您选择以何种方式解决此问题)并以最快的方式处理它。

This is a very rudimentary INNER JOIN :这是一个非常基本的INNER JOIN

SELECT
  products.name AS productname,
  price,
  agent.name AS agentname
  email
FROM 
  products
  INNER JOIN agent ON products.agentid = agent.userid

I recommend reviewing basic JOIN syntax and concepts.我建议查看基本的JOIN语法和概念。 Here's a link to Microsoft's documentation , though what you have above is pretty universal as standard SQL.这是指向 Microsoft 文档的链接,尽管您上面的内容与标准 SQL 一样通用。

Note that the INNER JOIN here assumes every product has an associated agentid that isn't NULL.请注意,这里的INNER JOIN假设每个产品都有一个不为 NULL 的关联agentid If there are NULL agentid in products , use LEFT OUTER JOIN instead to return even the products with no agent.如果products agentid为 NULL,则使用LEFT OUTER JOIN代替甚至返回没有代理的产品。

This is my join for slightly larger tables in Prod.Hope it helps.这是我对 Prod 中稍大表的连接。希望它有所帮助。

SELECT TOP 1000 p.[id]
      ,p.[attributeId]
      ,p.[name] as PropertyName
      ,p.[description]
      ,p.[active],
      a.[appId],
      a.[activityId],
      a.[Name] as AttributeName 
  FROM [XYZ.Gamification.V2B13.Full].[dbo].[ADM_attributeProperty] p
  Inner join [XYZ.Gamification.V2B13.Full].[dbo].[ADM_activityAttribute] a
  on a.id=p.attributeId
  where a.appId=23098;
select p.name productname, p.price, a.name as agent_name, a.email
from products p
inner join agent a on (a.userid = p.agentid)

If you don't want to use inner join (or don't have possibility to do it!) and would combine rows, you can use a cross join :如果您不想使用内部联接(或没有可能这样做!)并且要合并行,则可以使用交叉联接:

SELECT *
FROM table1
CROSS JOIN table2

or simply或者干脆

SELECT *
FROM table1, table2
select ProductName=p.[name]
, ProductPrice=p.price
, AgentName=a.[name]
, AgentEmail=a.email
from products p
inner join agent a on a.userid=p.agentid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM