1

嗨,我有一个数据库,可以逐行存储票务详细信息。因此,例如,如果有人进来并使用优惠券购买服务并购买了礼品卡,它将将该交易分解为 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”描述的票号。这可能吗,因为它们是具有相同票号的单独条目?

4

2 回答 2

1

尝试这样的事情(逻辑似乎有效,但可能有错字,因为我没有在具有您确切列名的表上尝试此操作):

SELECT *
FROM TicketDetails as t1
INNER JOIN TicketDetails as t2
ON t1.Ticket = t2.Ticket
WHERE t1.Description='BOGO' AND t2.Description='$5 OFF'

内连接应该创建描述的每一个组合,然后寻找一个同时具有 BOGO 描述和 $5 OFF 描述的行。

于 2012-08-02T01:14:00.807 回答
0
SELECT * 
FROM TicketDetails 
WHERE sTicket_Number = 'M123' AND 
    (Description ='BOGO' OR Description = '$5 OFF')

---- EDIT ----

Per the clarification in your comment you probably want this:

SELECT * 
FROM TicketDetails 
WHERE Description ='BOGO' OR Description = '$5 OFF'
于 2012-08-02T01:08:58.520 回答