使用一个查询的(多个)结果作为另一个查询的输入

Use (multiple) results from one query as input for another query

我正在使用 Vb.net 和 SQL

编写票务程序

我有 2 个 table 需要在查询中使用。 一张 table (Tickets) 包含我需要的所有值。 第二个 table (Ticketactions) 包含链接到 Ticket

的所有 TicketActions

我想要的是创建一个搜索,搜索以下列:

我有一个很好用的搜索并搜索门票 Table

        SELECT 
    tickets.TicketNumber, 
    tickets.Description,
    tickets.departmentname,
    tickets.devicename,
    tickets.creatorname,
    tickets.creationdate,
    tickets.ExecutionDate,
    tickets.closedate
    stateName,
    PriorityName,
    FullName,
         FROM tickets
    INNER Join Users On Tickets.AssignedTo = Users.UserID
    INNER Join States On Tickets.StateID = States.StateID 
    INNER Join Priorities On Tickets.priority = Priorities.PriorityID 
         WHERE Description Like '%SEARCHTEXT%' OR TicketNumber Like '%SEARCHTEXT%'

我想要的是使用作为 TicketActions table

中查询结果的额外 TicketNumbers 扩展上述查询
    SELECT TicketNumber
    FROM dbo.ticketaction
    WHERE ActionDescription like '%SEARCHTEXT%' 

因此结果应该与第一个查询完全相同,但添加了第二个查询找到的票号。

非常欢迎任何帮助,因为我真的不知道如何处理这个问题

你应该可以用一个简单的联合来做到这一点?以下是示例代码,但可能不是您想要的。

Union Syntax

SELECT tickets.TicketNumber
    ,tickets.Description
    ,tickets.departmentname
    ,tickets.devicename
    ,tickets.creatorname
    ,tickets.creationdate
    ,tickets.ExecutionDate
    ,tickets.closedate stateName
    ,PriorityName
    ,FullName
FROM tickets
INNER JOIN Users ON Tickets.AssignedTo = Users.UserID
INNER JOIN States ON Tickets.StateID = States.StateID
INNER JOIN Priorities ON Tickets.priority = Priorities.PriorityID
WHERE Description LIKE '%SEARCHTEXT%'
    OR TicketNumber LIKE '%SEARCHTEXT%'

UNION

SELECT t.TicketNumber
    ,NULL
    ,NULL
    ,NULL
    ,NULL
    ,NULL
    ,NULL
    ,NULL
    ,NULL
    ,NULL
FROM dbo.ticketaction t
WHERE ActionDescription LIKE '%SEARCHTEXT%'

要回答你的字面问题,应该是这样的……

SELECT 
    tickets.TicketNumber, 
    tickets.Description,
    tickets.departmentname,
    tickets.devicename,
    tickets.creatorname,
    tickets.creationdate,
    tickets.ExecutionDate,
    tickets.closedate
    stateName,
    PriorityName,
    FullName,
FROM
           tickets
INNER Join Users      On Tickets.AssignedTo = Users.UserID
INNER Join States     On Tickets.StateID = States.StateID 
INNER Join Priorities On Tickets.priority = Priorities.PriorityID 
WHERE tickets.Description Like '%SEARCHTEXT%'
   OR tickets.TicketNumber Like '%SEARCHTEXT%'
   OR tickets.TicketNumber IN (SELECT TicketNumber
                                 FROM dbo.ticketaction
                                WHERE ActionDescription like '%SEARCHTEXT%'
                              )


一个更好的解决方案,可能是在 ticketAction table

上使用 JOIN
SELECT 
    tickets.TicketNumber, 
    tickets.Description,
    tickets.departmentname,
    tickets.devicename,
    tickets.creatorname,
    tickets.creationdate,
    tickets.ExecutionDate,
    tickets.closedate
    stateName,
    PriorityName,
    FullName,
FROM
           tickets
INNER JOIN Users        ON tickets.AssignedTo = Users.UserID
INNER JOIN States       ON tickets.StateID = States.StateID 
INNER JOIN Priorities   ON tickets.priority = Priorities.PriorityID 
INNER JOIN ticketAction ON ticket.TicketNumber = ticketAction.TicketNumber
WHERE tickets.Description Like '%SEARCHTEXT%'
   OR tickets.TicketNumber Like '%SEARCHTEXT%'
   OR ticketAction.ActionDescription LIKE '%SEARCHTEXT%'

(假定每张票都有一个 ticketAction。如果不是这种情况,请使用 LEFT JOIN


或者,EXISTS()...

SELECT 
    tickets.TicketNumber, 
    tickets.Description,
    tickets.departmentname,
    tickets.devicename,
    tickets.creatorname,
    tickets.creationdate,
    tickets.ExecutionDate,
    tickets.closedate
    stateName,
    PriorityName,
    FullName,
FROM
           tickets
INNER Join Users      On Tickets.AssignedTo = Users.UserID
INNER Join States     On Tickets.StateID = States.StateID 
INNER Join Priorities On Tickets.priority = Priorities.PriorityID 
WHERE tickets.Description Like '%SEARCHTEXT%'
   OR tickets.TicketNumber Like '%SEARCHTEXT%'
   OR EXISTS (SELECT *
                FROM dbo.ticketaction
               WHERE ActionDescription like '%SEARCHTEXT%'
                 AND TicketNumber = tickets.TicketNumber
             )