简体   繁体   English

SQL Server:选择表中与其他表中的行共享值的行

[英]SQL Server: Select rows in table that share value with rows in another table

I have searched the questions but haven't been able to find an answer. 我已经搜索了问题,但找不到答案。 Perhaps my wording may be wrong. 也许我的措辞可能是错误的。 My problem is as follows: 我的问题如下:

Given table Users : 给定表Users

ID      code       owner
 1       777       James
 2       432      George
 3       111        Kale

And table Products : 和表产品

ID      product_name        code
 1             chair         777
 2             table         777
 3               fan         432
 4           monitor         777
 5              sofa         111
 6               bed         111

I need a query that fetches N number of rows from table Users and then fetches all rows from table Products that have a code matching the code of Any row fetched previously. 我需要一个查询,该查询从表Users中获取N个行,然后从表Products中获取所有行,这些产品的代码与以前获取的Any行的代码匹配。

Is this possible in SQL Server? 在SQL Server中可以吗? Is it optimal? 最佳吗?

For above example if i fetch first 2 rows (owners James and George) I should get all products with code 777 and 432. 对于上面的示例,如果我获取前两行(所有者James和George),则应获取所有代码为777和432的产品。

Yes it is a common thing, called "Joins" 是的,这很常见,称为“ Joins”

for example this would give you the answer: 例如,这将为您提供答案:

Select products.product_name,users.owner FROM products LEFT JOIN users ON products.code=users.code

You can read more about JOINS here: https://www.w3schools.com/sql/sql_join.asp 您可以在这里阅读有关JOINS的更多信息: https : //www.w3schools.com/sql/sql_join.asp

To select the first n (here 2 ) users use TOP . 要选择前n个 (此处为2个 )用户,请使用TOP Put that in a subquery and LEFT JOIN products on the common code to it. 将其放在子查询中,并在通用代码上添加LEFT JOIN products

SELECT *
       FROM (SELECT TOP 2
                    *
                    FROM users
                    ORDER BY id) u
            LEFT JOIN products p
                      ON p.code = u.code;

If you want to make users, that don't have at least one product, disappear you can replace the LEFT JOIN by an INNER JOIN . 如果您想让没有至少一种产品的用户消失,则可以用INNER JOIN代替LEFT JOIN INNER JOIN

Try this with Inner join : 尝试使用内部连接:

Select products.product_name,users.owner FROM products inner JOIN users ON products.code=users.code
where users.code in (777,432)

在此处输入图片说明

Create table #Users
(
    Id int Identity(1,1),
    Code varchar(100),
    Owner varchar(100)
)

Create table #Products
(
    Id int Identity(1,1),
    ProductName varchar(100),
    Code varchar(100)
)


insert Into #Users(Code,Owner)
Select '777','James'


insert Into #Users(Code,Owner)
Select '432','George'

insert Into #Users(Code,Owner)
Select '111','Kale'


insert Into #Products(ProductName,Code)
Select 'Chair','777'

insert Into #Products(ProductName,Code)
Select 'Table','777'

insert Into #Products(ProductName,Code)
Select 'fan','432'

insert Into #Products(ProductName,Code)
Select 'monitor','777'

insert Into #Products(ProductName,Code)
Select 'Sofa','111'

insert Into #Products(ProductName,Code)
Select 'bed','111'

select * from #products


select * from #Products Left join #Users on #Users.Code=#Products.Code Where Owner in ('James','George')

This works even if code is not unique; 即使代码不是唯一的,这仍然有效。

select * from Products where code in
 (
  select top 2 code from users order by id
 )

Replace 2 with the relevant N 用相关的N替换2

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

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