如何按2列搜索多个数据库条目

时间:2012-08-02 01:03:18

标签: sql sql-server database

您好我有一个数据库,可以逐行存储票证详细信息。因此,例如,如果有人进来购买服务,使用优惠券并且他们购买礼品卡,则将该交易分成3个单独的表条目...例如,如果我使用下面的查询搜索票号“m123”得到以下结果。

Select *
FROM            TicketsDetails
WHERE        (sTicket_Number = 'M123')

Ticket  Description Amount
M123    BOGO        15
M123    Service 1   -15
M123    GC Service 1    15
M123    $5 OFF      -5

当前查询和输出:

SELECT        t1.sTicket_Number, t1.sDescription, t1.sUserName, t2.sDescription AS Second_description
FROM            TicketsDetails AS t1 INNER JOIN
                         TicketsDetails AS t2 ON t1.sTicket_Number = t2.sTicket_Number
WHERE        (t1.sDescription = '$5 OFF') AND (t2.sDescription = 'BOGO')


M100304 $5 OFF  Kenny   BOGO
M100304 $5 OFF  Kenny   BOGO
M100542 $5 OFF  Kenny   BOGO
M103706 $5 OFF  Kenny   BOGO
M132464 $5 OFF  Dante   BOGO
M132464 $5 OFF  Dante   BOGO
M132501 $5 OFF  Dante   BOGO
M132501 $5 OFF  Dante   BOGO
M132528 $5 OFF  Dante   BOGO
M133892 $5 OFF  Raynon  BOGO
M133892 $5 OFF  Raynon  BOGO
M134067 $5 OFF  Raynon  BOGO
M134067 $5 OFF  Raynon  BOGO

我想做的是返回任何包含“bogo”和“$ 5 off”的票号。这可能是因为它们是具有相同票号的单独条目吗?

1 个答案:

答案 0 :(得分:0)

SELECT * 
FROM TicketDetails 
WHERE sTicket_Number = 'M123' AND 
    (Description ='BOGO' OR Description = '$5 OFF')

----编辑----

根据您的评论中的澄清,您可能想要这样:

SELECT * 
FROM TicketDetails 
WHERE Description ='BOGO' OR Description = '$5 OFF'