简体   繁体   中英

SQL Select inner join 2 columns

I have some tables, let's call them table UserManagement

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

And this is table 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 ?

Here's a query which splits the ToUser column from the Mailbox table in the 'mb_split_cte' CTE. Then it joins to the UserManagement table on Username. Then it returns the management positions of the ToUsers from the Mailbox table.

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. He recommends to use STRING_SPLIT function which is very good, but this function is unavailable in MS SQL Server 2014 and older. See: Compatibility Level

To get Mailbox data splitted by comma into rows per each ToUser , you can use 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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