简体   繁体   English

如何基于另一个具有多个值的表中的列值从表中选择行?

[英]How to select rows from a table based on a column value in another table that has multiple values?

I have two tables. 我有两张桌子。

User with columns UserId, Area 带有列UserId,区域的用户

The Area column is populated with multiple comma separated values (from listbox) - data looks like this: Area列填充有多个逗号分隔的值(来自列表框)-数据如下所示:

User1 KA,TN,AP,GJ
User2 MH,UP,MP,GJ

Order with columns OrderID, ProductID, Qty, Area 订单包含订单ID,产品ID,数量,区域

Data looks like this: 数据如下所示:

1 Prod1 10 GJ
2 Prod1 22 MH
3 Prod2 3  AP
4 Prod2 77 TN

How to select rows from Order table based on User table? 如何从基于User表的Order表中选择行?

Say logged in user is User1 . 说登录的用户是User1 His areas are KA,TN,AP,GJ . 他的领域是KA,TN,AP,GJ

The result should look like this: 结果应如下所示:

Prod1 10 GJ
Prod2 77 TN

Please advise on the SQL query to get this result. 请建议对SQL查询获取此结果。

Thanks and regards Krishna 感谢和问候克里希纳

Don't set this solution as a good practice because as all comments said above : 请勿将此解决方案设置为一种好的做法,因为正如上面所有评论所述
Never, ever store data as comma separated items. 永远不要将数据存储为逗号分隔的项目。

Workaround:- 解决方法:-

Manipulate the values that selected form user table for getting a good structure via using Replace function as next demo:- 通过使用Replace功能作为下一个演示,操纵从user表中选择的值以获得良好的结构:-

Create table #user (username varchar(10), Areas varchar (100))
go
insert into #user values ('User1', 'KA,TN,AP,GJ')
insert into #user values ('User2', 'MH,UP,MP,GJ')
go
Create table #order ( OrderID int , ProductID varchar(10), Qty int , Area char (2))
go
insert into #order values (1, 'Prod1', 10, 'GJ')
insert into #order values (2, 'Prod1', 22, 'MH')
insert into #order values (3, 'Prod2', 3,  'AP')
insert into #order values (4, 'Prod2', 77, 'TN')
go 

declare @List nvarchar(250)

select @List = '('''+(select Areas from #user where username = 'User1') + ''')'
select @List = replace(@List,',',''',''')


exec ('select ProductID,Qty, area from #order
where Area in' +  @List )

drop table #user
drop table #order

Result:- 结果:-

在此处输入图片说明

Note: I assumed the 3rd record is missing in your desired result set by mistake. 注意:我认为您期望的结果集中错误地缺少了第三条记录。

you can use patindex 您可以使用patindex

select o.*
from u
join o on patindex('%,'+o.Area+',%', ','+u.area+',')>0
where userid= 'User1'

Store data as comma separated items is not a good practice. 将数据存储为逗号分隔的项目不是一个好习惯。 But you can try something like below 但是您可以尝试以下操作

DECLARE @V_USER NVARCHAR(100)   ='User1'

SELECT  *
FROM    Order   AS  O
    INNER JOIN User AS U    ON U.Area LIKE '%'+O.Area+'%'
WHERE   U.UserId    =   @V_USER

暂无
暂无

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

相关问题 MySql - 如何根据在另一个表中加入的多个值从一个表中 select 行 - MySql - how to select rows from one table based on multiple values joined in another table 如何基于另一列中具有不同值的列从表中选择唯一记录 - how to select unique records from a table based on a column which has distinct values in another column Select 一个表中的行,基于另一个表中的列值? - Select rows in one table, based on column values in another table? 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column 如何基于Oracle中另一个表中的行的值选择列 - how to select a column based on the value of a row from another table in oracle 根据另一张表中的列值转换一张表中的行 - Transposing rows from one table based on column values in another table SQL Server:如何在列中选择具有相同值的行,但在另一列上为分组行选择一些精确值 - SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows 如何重复根据列值选择行,并且在另一列中具有不同的值 - How to select rows based on column value is repeating and has different values in another column 如何根据另一个表中多个列的值从一个表中提取多行,然后在 SQL 中连接? - How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL? 根据同一表中另一列中的唯一值更新多行 - Update multiple rows based on unique values in another column in same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM