我正在使用交叉联接来复制从我的数据库输出的行。
我想要一个过滤器,以便只复制某些值
这是我到目前为止所拥有的
SELECT EmployeeID, EmployeeForeName From Employee
CROSS JOIN
(SELECT TOP 2 Number FROM Numbers) Numbers
WHERE EmployeeID in (20,30)
输出为
EmployeeID EmployeeForeName
20 Jamie
30 Karl
20 Jamie
30 Karl
我该如何做,但只复制EmployeeID = 20;导致以下
EmployeeID EmployeeForeName
20 Jamie
30 Karl
20 Jamie
谢谢!
答案 0 :(得分:1)
试试这个:
SELECT e.EmployeeID, e.EmployeeForeName
FROM Employee AS e
CROSS APPLY
(SELECT t.n
FROM (VALUES (1), (2)) AS t(n)
WHERE (t.n = 1) OR (e.EmployeeID = 20)) AS x
WHERE e.EmployeeID in (20,30)
答案 1 :(得分:0)
您似乎不希望两个值都有cross join
。
SELECT EmployeeID, EmployeeForeName
FROM Employee CROSS JOIN
(SELECT TOP 2 Number FROM Numbers) Numbers
WHERE EmployeeID IN (20)
UNION ALL
SELECT EmployeeID, EmployeeForeName
FROM Employee
WHERE EmployeeID IN (30);
答案 2 :(得分:0)
我知道您只希望重复EmployeeId 20,而不是30,我不知道Numbers表中的内容,我想在下面做
DECLARE @TBL AS TABLE
(
EmployeeID INT, EmployeeForeName VARCHAR(100)
)
INSERT INTO @TBL
SELECT 20 EmployeeID,'Jamie' EmployeeForeName
UNION ALL
SELECT 20,'Jamie'
UNION ALL
SELECT 30,'Karl'
UNION ALL
SELECT 30,'Karl'
select * from @TBL where EmployeeID =20
union all
select distinct * from @TBL where EmployeeID =30
答案 3 :(得分:-1)
试试这个
SELECT EmployeeID, EmployeeForeName From Employee
CROSS JOIN
(SELECT TOP 2 Number FROM Numbers) Numbers
WHERE EmployeeID =20
union all
SELECT EmployeeID, EmployeeForeName
FROM Employee
WHERE EmployeeID =30
答案 4 :(得分:-2)
这样的事情可以解决问题;
SELECT EmployeeID, EmployeeForeName From Employee
CROSS JOIN
(SELECT TOP 2 Number FROM Numbers) Numbers
WHERE EmployeeID = 20
UNION ALL
SELECT EmployeeID, EmployeeForeName From Employee
WHERE EmployeeID = 30