嗯,我之前发布了一个类似的问题,并得到了一个解决方案,但我正在做类似的事情和一点点不同,我需要一些帮助。
有一个搜索者表,雇主表,类别表,兴趣表......
FK_is_id = Seeker ID
FK_ie_id = Employer ID
FK_ic_id = Category ID
兴趣表数据:
int_id FK_is_ID FK_ie_ID FK_ic_ID
1 2 null 3
2 2 null 5
3 null 1 3
4 null 3 1
5 null 2 5
6 4 null 1
我想要做的是,过滤掉与雇主(eid)有相同兴趣的雇主(FK_ie_ID),像竞争对手或竞争对手。
使用以下查询,我能够做我想要的,问题是,它还在结果中重新登录用户....我只是登录用户以外的雇主..
SELECT ins.int_id, ins.FK_is_ID, ine.FK_ie_ID,ins.FK_ie_ID,emp.e_name,ine.int_id
FROM Interests ins
INNER JOIN Interests ine ON ins.FK_ic_ID = ine.FK_ic_ID
FULL OUTER JOIN employers emp ON emp.e_id=ine.FK_ie_id
WHERE ins.FK_ie_ID = @eid
AND ine.FK_ie_ID IS NOT null
答案 0 :(得分:0)
我认为以下内容可以完成您的结果,您可以验证:
DECLARE @eid INT
SELECT @eid = 3
DECLARE @Interest TABLE
(
int_id INT NOT NULL,
FK_is_ID INT NULL,
FK_ie_ID INT NULL,
FK_ic_ID INT NULL
)
DECLARE @Employers TABLE
(
e_id INT NOT NULL,
e_name VARCHAR(MAX) NULL
)
INSERT INTO @Interest
SELECT * FROM
(
SELECT 1 AS int_id, 2 AS FK_is_ID, NULL AS FK_ie_ID, 3 AS FK_ic_ID
UNION ALL
SELECT 2 AS int_id, 2 AS FK_is_ID, NULL AS FK_ie_ID, 5 AS FK_ic_ID
UNION ALL
SELECT 3 AS int_id, NULL AS FK_is_ID, 1 AS FK_ie_ID, 3 AS FK_ic_ID
UNION ALL
SELECT 4 AS int_id, NULL AS FK_is_ID, 3 AS FK_ie_ID, 1 AS FK_ic_ID
UNION ALL
SELECT 5 AS int_id, NULL AS FK_is_ID, 2 AS FK_ie_ID, 5 AS FK_ic_ID
UNION ALL
SELECT 6 AS int_id, 4 AS FK_is_ID, NULL AS FK_ie_ID, 1 AS FK_ic_ID
) AS DATA
INSERT INTO @Employers
SELECT * FROM
(
SELECT 1 AS e_id, 'one' AS e_name
UNION ALL
SELECT 2 AS e_id, 'two' AS e_name
UNION ALL
SELECT 3 AS e_id, 'three' AS e_name
) AS DATA
SELECT
Interest.int_id,
Interest.FK_is_ID,
Employers.e_name
FROM
@Interest AS Interest
LEFT OUTER JOIN
@Employers AS Employers
ON Interest.FK_ie_id = Employers.e_id
INNER JOIN
(
SELECT
InterestSub.FK_ic_ID
FROM
@Interest AS InterestSub
WHERE
InterestSub.FK_ie_ID = @eid
) AS SubData
ON Interest.FK_ic_ID = SubData.FK_ic_ID
WHERE
COALESCE(Interest.FK_ie_ID, '') <> @eid
&#13;