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

时间:2018-11-21 13:25:49

标签: sql sql-server multiple-results

我正在使用Vb.net和SQL编写票务程序

我有2个表需要在查询中使用。 一张桌子(门票)保存着我需要的所有值。 第二张表(Ticketactions)包含链接到票证的所有TicketActions

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

  • Ticket.description
  • ticket.ticketID
  • Ticketaction.ActionDescrition

我的搜索效果很好,并搜索了票证表

        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%'

我想要在上方查询中扩展更多的TicketNumbers,这些是TicketActions表中查询的结果

    SELECT TicketNumber
    FROM dbo.ticketaction
    WHERE ActionDescription like '%SEARCHTEXT%' 

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

任何帮助都将受到欢迎,因为我真的不知道该如何处理

2 个答案:

答案 0 :(得分:2)

要回答您的字面问题,它类似于...

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%'
                              )


一个更好的解决方案是在JOIN表上使用ticketAction

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
             )

答案 1 :(得分:0)

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

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%'