我有3张桌子:
表Position
:
KodePosition | NamePosition | UserLogin
========================================
0037 Master A winz\alfa
0038 Master B winz\beta
0043 Master C winz\carlie
表UserBackup
(PJS):
KodePosition | UserOrigin | UserChange | StartDate | EndDate
================================================================
0037 winz\alfa winz\carlie 10-10-2014 17-10-2014
表History
:
IdHistory | KodePosition | StartDate | EndDate | User | Comment
===============================================================================
19F5FCFC 0038 14-10-2014 14-10-2014 winz\beta i not agree...
19F5FCFC 0043 15-10-2014 15-10-2014 winz\carlie i agree...
我想显示如下数据:
Name | Date | Position | Comment
===================================================
winz\beta 14-10-2014 Master B i not agree...
winz\carlie 15-10-2014 Master A i agree...
描述:
目前,我有一个这样的存储过程,但不显示我需要的数据。
select
A.IdHistory, A.StartDate, B.NamePosition, B.UserLogin, A.Comment
from
History as A
left join
Position as B on A.KodePosition = B.KodePosition
Where
A.IdHistory = '19F5FCFC'
order by
A.StartDate asc
请帮帮我们......谢谢......
答案 0 :(得分:0)
如果你想问三个表,你可以这样做(抱歉没有使用你的数据,但我不理解它们的连接(或你的解释))。
SELECT a.Column1, a.Column2, b.Column1
FROM table1 AS a
LEFT JOIN table2 AS b
ON a.Column1=b.Column1
WHERE (
SELECT c.Column1
FROM table3 AS c
INNER JOIN table2 as b
ON b.Column1=c.Column1
WHERE c.Column2 LIKE 'MyTarget%');
你必须考虑两个SELECT,不要试图把所有东西都放在一个。顺便说一句。大多数DB不支持多联接。
答案 1 :(得分:0)
如果没有关于你要做什么的更多信息,这在黑暗中有点刺痛......但根据你的描述,我相信你试图首先过滤UserBackup(PJS)中的行表基于相应的History.KodePosition记录是否属于相应UserBackup(PJS)记录的StartDate和EndDate。然后,根据返回的KodePositions,您希望从Position表中检索相关记录。我不确定这是您正在寻找的全貌,但希望这会让您更进一步:
;WITH cteData
AS (
SELECT u.KodePosition, h.IDHistory, h.StartDate, h.Comment
FROM UserBackup(PJS) u
INNER JOIN History h ON h.User = u.UserChange AND (h.StartDate >= u.Startdate and h.StartDate <= u.EndDate)
)
SELECT c.IDHistory, c.StartDate, p.NamePostition, p.UserLogin, c.Comment
FROM Position p
INNER JOIN cteData c ON c.KodePosition = p.KodePosition