从没有任何id的表中检索数据

时间:2013-08-21 09:08:49

标签: sql-server

我有这样的查询:

SELECT
    t.transactID,
    t.TBarcode,
    l.LocName,
    v.Vtype,
    t.dtime,
    t.PlateNo,
    ps.PS,
    pc.PlateCode,
    p.COMMENTS
FROM dbo.PoliceRecord_tbl p
INNER JOIN dbo.Transaction_tbl t ON t.PSID = p.PSource AND t.PCdID = p.PSCode AND t.PlateNo = p.PNumber
JOIN dbo.Location_tbl l ON l.Locid = t.Locid
JOIN dbo.VType_tbl v ON v.vtid = t.vtid
JOIN dbo.PlateSource_tbl ps ON ps.PSID = t.PSID
JOIN dbo.PlateCode_tbl pc ON pc.PCdID = t.PCdID AND t.STATUS IN (0, 1)
    AND t.TBarcode NOT IN (
        SELECT TBarcode
        FROM dbo.EmailSendLog_tbl
    ) AND p.DELETED = 0
END

我的出局变得正确: enter image description here

但我还有一个表EmailSubject_tbl只包含一个字段enter image description here 我想在执行查询时也获得此值: 我的预期输出

Transactid  Tbarcode    Locname       Vtype    Dtime      PlateNo PS PlateCode Comments EmailSubject
3701      6191112123456 AddressHotel  Normal   2013-06-09  123456  Dubai  A     Test     PoliceRecord

3 个答案:

答案 0 :(得分:1)

假设您的EmailSubject包含一条记录

SELECT
    t.transactID,
    t.TBarcode,
    l.LocName,
    v.Vtype,
    t.dtime,
    t.PlateNo,
    ps.PS,
    pc.PlateCode,
    p.COMMENTS,
    (select TOP 1 EmailSubject FROM EmailSubject_tbl ) AS EmailSubject
FROM dbo.PoliceRecord_tbl p
INNER JOIN dbo.Transaction_tbl t ON t.PSID = p.PSource AND t.PCdID = p.PSCode AND t.PlateNo = p.PNumber
JOIN dbo.Location_tbl l ON l.Locid = t.Locid
JOIN dbo.VType_tbl v ON v.vtid = t.vtid
JOIN dbo.PlateSource_tbl ps ON ps.PSID = t.PSID
JOIN dbo.PlateCode_tbl pc ON pc.PCdID = t.PCdID AND t.STATUS IN (0, 1)
    AND t.TBarcode NOT IN (
        SELECT TBarcode
        FROM dbo.EmailSendLog_tbl
    ) AND p.DELETED = 0
END

答案 1 :(得分:0)

您可以使用cross join引入不相关的表格:

SELECT
    ...
    p.COMMENTS,
    es.EmailSubject
FROM dbo.PoliceRecord_tbl p
....
CROSS JOIN dbo.EmailSubject_tbl es
...

答案 2 :(得分:-1)

试试这个

SELECT
    t.transactID,
    t.TBarcode,
    l.LocName,
    v.Vtype,
    t.dtime,
    t.PlateNo,
    ps.PS,
    pc.PlateCode,
    p.COMMENTS,
   e.EmailSubject
FROM dbo.PoliceRecord_tbl p, EmailSubject_tbl e
INNER JOIN dbo.Transaction_tbl t ON t.PSID = p.PSource AND t.PCdID = p.PSCode AND t.PlateNo = p.PNumber
JOIN dbo.Location_tbl l ON l.Locid = t.Locid
JOIN dbo.VType_tbl v ON v.vtid = t.vtid
JOIN dbo.PlateSource_tbl ps ON ps.PSID = t.PSID
JOIN dbo.PlateCode_tbl pc ON pc.PCdID = t.PCdID AND t.STATUS IN (0, 1)
    AND t.TBarcode NOT IN (
        SELECT TBarcode
        FROM dbo.EmailSendLog_tbl
    ) AND p.DELETED = 0