我正在处理一个SQL查询,其中数据存储在表r.
中基于give
或receive
的列,我需要将数据存储在临时表中然后存储在新表中。
本质上,数据是从3个表中提取的,最终目标是获取图像的二进制代码并存储以显示在.net
我正在尝试找出多重选择
因此give
中的receive
或r
等于Username
中的S
,如果employee
,则显示所有相关数据并获取图像等于employee
我已经尝试了很多代码,并且觉得OR
可以解决问题,但似乎没有。
感谢您提供的任何帮助。
SELECT
r.id, r.give, r.[receive], r.[type], r.[description], r.isApproved,
r.photoGive, r.photoReceive
INTO
#temp2
FROM
Intranet.dbo.Recgonize r
SELECT s.Employee, s.Username
INTO #temp3
FROM Vision7.dbo.SEUser s
SELECT p.Photo, p.Employee
INTO #temp4
FROM Vision7.dbo.EMPhoto p
SELECT *
FROM #temp2 AS a
INNER JOIN #temp3 b ON a.give = b.Username
INNER JOIN #temp4 c ON b.Employee = c.Employee
DROP TABLE #temp2
DROP TABLE #temp3
DROP TABLE #temp4
答案 0 :(得分:0)
您可能需要尝试以下操作:
-- Include only required columns select rather than "*"
SELECT *
FROM #temp2 AS a
INNER JOIN #temp3 b ON a.give = b.Username
INNER JOIN #temp3 b2 ON a.receive = b2.Username
INNER JOIN #temp4 c ON b.Employee = c.Employee
INNER JOIN #temp4 c2 ON b2.Employee = c2.Employee
或
-- Include only required columns in select rather than "*"
SELECT *
FROM
(select r.id, r.give as UserName, r.[type], r.[description], r.isApproved, r.photoGive from #temp2
union
select r.id, r.[receive], r.[type], r.[description], r.isApproved, r.photoReceive from #temp2
) AS a
INNER JOIN #temp3 b ON a.UserName = b.Username
INNER JOIN #temp4 c ON b.Employee = c.Employee
如果临时表未用于应用程序的必要需求,则相同的逻辑可以与实际表一起使用,只需将#Temp2,#Temp3,#Temp4替换为适当的表名即可。
答案 1 :(得分:0)
尝试使用以下单个脚本-
SELECT a.id,
a.give,
a.[receive],
a.[type],
a.[description],
a.isApproved,
a.photoGive,
a.photoReceive,
b.Employee,
b.Username,
c.Photo,
c.Employee
FROM Intranet.dbo.Recgonize A
INNER JOIN Vision7.dbo.SEUser B ON a.give = b.Username
INNER JOIN Vision7.dbo.EMPhoto C ON b.Employee = c.Employee;
答案 2 :(得分:0)
为什么要为此使用临时表?这些只会使代码更难调试和维护,运行起来更加昂贵,并且理解起来也更加复杂。
JOIN
可以在没有临时表的情况下使用。但是,您确实需要附加的逻辑才能在不同的列中获取赋予和接收的值,因此需要更多JOIN
:
SELECT r.id, r.give, r.[receive], r.[type], r.[description],
r.isApproved, r.photoGive, r.photoReceive,
pg.Photo as give_photo, pg.Employee as give_employee,
pr.Photo as receive_photo, pr.Employee as receive_employee
FROM Intranet.dbo.Recognize r LEFT JOIN
Vision7.dbo.SEUser sg
ON r.give = sg.Username LEFT JOIN
Vision7.dbo.SEUser sr
ON r.receive = sr.Username LEFT JOIN
Vision7.dbo.EMPhoto pg
ON sg.Employee = pg.Employee LEFT JOIN
Vision7.dbo.EMPhoto pr
ON sr.Employee = pr.Employee