简体   繁体   English

SQL 选择内连接 2 列

[英]SQL Select inner join 2 columns

I have some tables, let's call them table UserManagement我有一些表,我们称它们为表UserManagement

ID  Username Position
1   Bryan    Client
2   Frey     Client
3   Will     Vendor
4   Anne     Vendor
5   Belle    Vendor

And this is table Mailbox这是表Mailbox

ID  FromUser ToUser           Message
1   Bryan    Will,Anne,Belle  Hai
2   Anne     Bryan,Frey       Hello

I've been try this:我一直在尝试这个:

select * from Mailbox t
inner join (select Username from UserManagement where Position = 'Client' group by Username)um
on t.ToUser = um.Username

But it's work when column ToUser contain only Username Bryan or Frey not with Bryan,Frey How to solve that when column ToUser contain Username like Bryan,Frey ?但它的工作时,列ToUser只包含用户名Bryan或者Frey不与Bryan,Frey如何解决,当列ToUser包含用户名像Bryan,Frey

Here's a query which splits the ToUser column from the Mailbox table in the 'mb_split_cte' CTE.这是一个查询,它从“mb_split_cte”CTE 的邮箱表中拆分 ToUser 列。 Then it joins to the UserManagement table on Username.然后它连接到用户名上的 UserManagement 表。 Then it returns the management positions of the ToUsers from the Mailbox table.然后它从邮箱表中返回 ToUsers 的管理职位。

Data数据

DECLARE @Mailbox TABLE(ID int identity(1,1),
                       FromUser varchar(150),
                       ToUser varchar(150),
                       [Message] varchar(250));

INSERT INTO @Mailbox(FromUser, ToUser, [Message])
VALUES('Bryan', 'Will,Anne,Belle', 'Hai'),
('Anne', 'Bryan,Frey', 'Hello');

DECLARE @UserManagement TABLE(ID int identity(1,1),
                       Username varchar(150),
                       Position varchar(150));

INSERT INTO @UserManagement(Username, Position) VALUES
('Bryan', 'Client'),
('Frey', 'Client'),
('Will', 'Vendor'),
('Anne', 'Vendor'),
('Belle', 'Vendor');

Query询问

;with mb_split_cte(ID, FromUser, ToUser, [Message]) as (
    select ID, FromUser, sp.[value], [Message]
    from @Mailbox m
         cross apply string_split(m.ToUser, ',') sp)
select msc.*, um.Position as ToUserPosition
from mb_split_cte msc
     join @UserManagement um on msc.ToUser=um.Username;

Output输出

ID  FromUser    ToUser  Message ToUserPosition
1   Bryan       Will    Hai     Vendor
1   Bryan       Anne    Hai     Vendor
1   Bryan       Belle   Hai     Vendor
2   Anne        Bryan   Hello   Client
2   Anne        Frey    Hello   Client

First of all, please read Dan's Guzman comment to your question.首先,请阅读 Dan 对您的问题的 Guzman 评论。 He recommends to use STRING_SPLIT function which is very good, but this function is unavailable in MS SQL Server 2014 and older.他推荐使用STRING_SPLIT 函数,非常好,但该函数在 MS SQL Server 2014 及更早版本中不可用。 See: Compatibility Level请参阅: 兼容级别

To get Mailbox data splitted by comma into rows per each ToUser , you can use CTE .要按每个ToUserMailbox数据按逗号拆分为行,您可以使用CTE See:看:

DECLARE @Mailbox TABLE(ID int identity(1,1),  FromUser varchar(150),  ToUser varchar(150), [Message] varchar(250))
INSERT INTO @Mailbox(FromUser, ToUser, [Message])
VALUES('Bryan', 'Will,Anne,Belle', 'Hai'),
('Anne', 'Bryan,Frey', 'Hello')

;WITH CTE AS 
(
    --initial query
    SELECT ID, FromUser, LEFT(ToUser, CHARINDEX(',', ToUser)-1) ToUser, RIGHT(ToUser, LEN(ToUser) - CHARINDEX(',', ToUser)) remainder,  [Message]
    FROM @Mailbox
    WHERE CHARINDEX(',', ToUser)>0
    --recursive part
    UNION ALL
    SELECT ID, FromUser, LEFT(remainder, CHARINDEX(',', remainder)-1) ToUser, RIGHT(remainder, LEN(remainder) - CHARINDEX(',', remainder)) remainder,  [Message]
    FROM CTE
    WHERE CHARINDEX(',', remainder)>0
    UNION ALL
    SELECT ID, FromUser, remainder ToUser, NULL remainder,  [Message]
    FROM CTE
    WHERE CHARINDEX(',', remainder)=0
)
SELECT ID, FromUser, ToUser, [Message]
FROM CTE 
ORDER BY ID 

Above query produces:上面的查询产生:

ID  FromUser    ToUser  Message
1   Bryan   Will    Hai
1   Bryan   Anne    Hai
1   Bryan   Belle   Hai
2   Anne    Bryan   Hello
2   Anne    Frey    Hello

I have used your SQL code and modified it slightly.我已经使用了你的 SQL 代码并稍微修改了它。 Why dont you try it like this?你为什么不试试呢?

select * from Mailbox t
inner join (select Username from UserManagement where Position = 'Vendor' group by Username)um
on t.ToUser like '%'+um.Username

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

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