我有一个烦人的问题,我无法逾越。
我有一个分为两个表的照片数据库:
表1(taba.col
)包含四列:
SQL> with
2 taba (id, col) as
3 (select 1, '0493484402' from dual union all
4 select 2, '012345' from dual
5 ),
6 tabb (id, col) as
7 (select 1, 'addr_mastersubscription has changed from 32488141893 to 32488141973' from dual union all
8 select 2, 'nothing changed from 098776 to 012345' from dual
9 )
10 --
11 select a.id,
12 case when a.col = regexp_substr(b.col, '\d+', 1, 2) then a.col || ' exists in tabb'
13 else a.col || ' does not exist in tabb'
14 end result
15 from taba a join tabb b on a.id = b.id;
ID RESULT
---------- ---------------------------------
1 0493484402 does not exist in tabb
2 012345 exists in tabb
SQL>
表2(snaps
)成对显示,其中包含
'photoid', 'filename', 'location', 'created'
表2显示了照片对,因为beforeid和afterid使用表1中的唯一photoid INT。看起来很简单。
但如果beforeid和afterid的位置相同,那么我提出的查询之一(按位置)将重复。例如:-
befter
当照片位置不同时,很好,但是如果它们相同则不可以。我试过添加DISTINCT等,但无法弄清楚。
有什么想法吗? P
答案 0 :(得分:0)
您必须告诉SQL语句您将从给定表中获取什么。如果您在两个表上都加一个*,那么您将得到重复的结果。
尝试一下:
SELECT
snaps.photoid, snaps.filename, snaps.location, snaps.created, befter.ppairid,
befter.beforeid, befter.afterid, befter.description
FROM
befter
INNER JOIN
snaps
ON
snaps.photoid = befter.ppairid
WHERE
snaps.location='oxford'
AND
(snaps.photoid = befter.beforeid OR snaps.photoid = befter.afterid)
答案 1 :(得分:0)
使用两个left join
:
select b.*, sb.*, sa.*
from befter b left join
snaps sb
on sb.photoid = b.beforeid and sb.location = 'oxford' left join
snaps sa
on sa.photoid = b.afterid and sa.location = 'oxford'
where sb.photoid is not null or sa.photoid is not null;
这包括结果集中的前后快照。