试图显示与表3相关的表1的ID

时间:2019-04-04 18:33:41

标签: sql postgresql join

我有三个表:渗透,phishkit,快照。我们需要能够查询exfillocation.filename并打印相关的snapshot.id,这需要遍历phishkit表。

exfillocation.phishkit_id与phishkit.id作为外键相关。

表渗透模式:

id  exfil_location    phishkit_id
==  =========         ============
1   ['open.txt']       7442      
2   ['bot.txt']        9931

phishkit.snapshot_id与snapshot.id作为外键相关。

Phishkit模式:

id     snapshot_id     md5
===    ============   =====
7442      1492          f4a3954e39b90c02f4a3954e39b90c02
9931      1661          e048f240ad0845b50abe8df9124ce3fb

快照架构:

id     asn           url
===    ======        =============
1661    123          badwebsite.malicious.com
1492    31           haxx0rs.hacking.com

我尝试读取postgresql的四个不同的JOIN方法以及UNION方法,但是我似乎没有得到返回的snapshot_id列。

我尝试了一些尴尬的事情:

SELECT exfil_location, found_in_file, phishkit_id
    FROM public.lookup_exfillocation
    FULL OUTER JOIN public.lookup_phishkit
    ON public.lookup_exfillocation.phishkit_id = public.lookup_phishkit.id
    FULL OUTER JOIN public.lookup_snapshot
    ON public.lookup_phishkit.snapshot_id = public.lookup_snapshot.id WHERE exfil_location::text NOT LIKE ('__script.txt__') ORDER BY phishkit_id;

我希望看到相关的lookup_snapshot.id和相关的lookup_phishkit.id,但均未显示。

Results not showing the related snapshot.id column

1 个答案:

答案 0 :(得分:0)

我不小心找到了解决方案。归结为我正在选择的列。使用*显示JOIN语句中的所有列。然后,我从所需的列中进行选择。查询如下:

    FROM public.lookup_exfillocation
    FULL OUTER JOIN public.lookup_phishkit
    ON public.lookup_exfillocation.phishkit_id = public.lookup_phishkit.id
    FULL OUTER JOIN public.lookup_snapshot
    ON public.lookup_phishkit.snapshot_id = public.lookup_snapshot.id WHERE exfil_location::text NOT LIKE ('__script.txt__') ORDER BY phishkit_id;```