我在Oracle中有两个表
TABLE A ( all fields are primary keys) ----------------------------------------- SID (INT) SDAY (CHAR) DATETIME (DATETIME)
TABLE B ( all fields are primary keys- same as table A) --------------------------------------------------------- SID SDAY DATETIME
我想找到 表A中的记录
where A.SID = B.SID
AND A.SDAY = B.SDAY
AND A.DATETIME <> B.DATETIME
(即 - 我想在表A中找到与表B中除日期时间之外的相应列匹配的记录)
直接INNER JOIN SQL没有给我结果。有什么想法吗?
这也无效
SELECT A.* FROM TABLE_A A
WHERE EXISTS (
SELECT 'X' FROM TABLE_B B
WHERE A.SID = B.SID
AND A.SDAY = B.SDAY
AND A.DATETIME <> B.DATETIME)
答案 0 :(得分:0)
我可能会把它作为一个内部联接而不是一个存在,但我希望你的工作。
SELECT A.*
FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.SID = B.SID
AND A.SDAY = B.SDAY
AND A.DATETIME <> B.DATETIME
如果您仍未获得结果,可以尝试发布一些样本数据供我们测试
您可以运行以完整测试以显示上述联接有效:
CREATE TABLE #Table_a
(
[SID] INT,
SDAY CHAR,
[DATETIME] DATETIME
)
CREATE TABLE #Table_b
(
[SID] INT,
SDAY CHAR,
[DATETIME] DATETIME
)
INSERT INTO #Table_a
( SID, SDAY, DATETIME )
VALUES ( 0, -- SID - int
'A', -- SDAY - char
'2012-12-27 23:00:01' -- DATETIME - datetime
)
INSERT INTO #Table_a
( SID, SDAY, DATETIME )
VALUES ( 2, -- SID - int
'B', -- SDAY - char
'2012-12-27 23:00:01' -- DATETIME - datetime
)
INSERT INTO #Table_a
( SID, SDAY, DATETIME )
VALUES ( 3, -- SID - int
'C', -- SDAY - char
'2012-12-27 23:00:01' -- DATETIME - datetime
)
INSERT INTO #Table_B
( SID, SDAY, DATETIME )
VALUES ( 0, -- SID - int
'A', -- SDAY - char
'2012-12-27 3:00:01' -- DATETIME - datetime
)
INSERT INTO #Table_B
( SID, SDAY, DATETIME )
VALUES ( 2, -- SID - int
'B', -- SDAY - char
'2012-12-27 3:00:01' -- DATETIME - datetime
)
INSERT INTO #Table_B
( SID, SDAY, DATETIME )
VALUES ( 3, -- SID - int
'C', -- SDAY - char
'2012-12-27 23:00:01' -- DATETIME - datetime
)
SELECT A.*
FROM #TABLE_A A
INNER JOIN #TABLE_B B
ON A.SID = B.SID
AND A.SDAY = B.SDAY
AND A.DATETIME <> B.DATETIME
DROP TABLE #Table_a
DROP TABLE #Table_b
答案 1 :(得分:0)
如果您显示如下表格定义,也许您的问题会更容易阅读:
- 我在Oracle中有两个表
CREATE TABLE TABLE_A -- BTW: why the caps?
( SID INTEGER NOT NULL
, SDAY CHAR NOT NULL
, ZDATETIME DATETIME NOT NULL -- note: using a typename as an identier
-- will break things.
, PRIMARY KEY (SID,SDAY,ZDATETIME)
);
CREATE TABLE TABLE_B -- all (1)fields are primary keys
( SID SDAY DATETIME NOT NULL PRIMARY KEY
);
[也许这应该是评论,但评论没有格式化的可能性]
(即 - 我想在表A中找到与表B中相应列相匹配的记录
EXISTS
除日期时间外)
NOT EXISTS
好的,现在解决方案:
SELECT A.* FROM TABLE_A A
WHERE EXISTS (
SELECT 1 FROM TABLE_B B
WHERE A.SID = B.SID
AND A.SDAY = B.SDAY
)
AND NOT EXISTS (
SELECT 2 FROM TABLE_B B
WHERE A.SID = B.SID
AND A.SDAY = B.SDAY
AND A.ZDATETIME = B.ZDATETIME
)
;