用户表account(account_id/帐号id,account_friend/好友列表,值形式为 account_id+"|"来分割)
包含数据:
account_id account_friend
1 2|4|
2 1|
3 1|
4 1|2|3|
-------------------------------------------------------------------------
信息表 opp(account_id/发布者帐号,titles/信息标题)
包含数据:
account_id titles
1 信息A
1 信息B
因为opp里信息是account_id=1的会员发布的,而这个会员又有2|4|这个好友
,所以想实现一种方法(既所谓的信息共享)来实现如下的搜索结果
1 信息A
1 信息B
2 信息A
2 信息B
4 信息A
4 信息B
请问高手用什么方法能实现以下结果呢
以下語句測試OK
Select * From opp
Union
Select B.account_id, C.titles From account A Inner Join account B On CharIndex('|' + Cast(B.account_id As Varchar) + '|', '|' + A.account_friend) > 0 Inner Join opp C On A.account_id = C.account_id
--創建測試環境
Create Table account(
account_id Int,
account_friend Varchar(100))
Insert account Select 1, '2|4|'
Union All Select 2, '1|'
Union All Select 3, '1|'
Union All Select 4, '1|2|3|'
Create Table opp(
account_id Int,
titles Nvarchar(20))
Insert opp Select 1, N'信息A'
Union All Select 1, N'信息B'
GO
--測試
Select * From opp
Union
Select B.account_id, C.titles From account A Inner Join account B On CharIndex('|' + Cast(B.account_id As Varchar) + '|', '|' + A.account_friend) > 0 Inner Join opp C On A.account_id = C.account_id
GO
--刪除測試環境
Drop Table account, opp
--結果
/*
account_idtitles
1信息A
1信息B
2信息A
2信息B
4信息A
4信息B
*/

浙公网安备 33010602011771号