Shmewnix
Shmewnix

Reputation: 1573

How to search multple database entries by 2 columns

Hi I have a database that stores ticket details line by line. So for example if someone comes in and purchases a service, with a coupon and they purchase a gift card it breaks that transaction down into 3 seperate table entries... For example if I search the ticket number "m123" with the query below I get the results below.

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

Current query and output:

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

What I'd like to do is return any ticket number that has the description "bogo" and "$5 off". Is this possible since they are seperate entries with the same ticket number?

Upvotes: 1

Views: 71

Answers (2)

John Girata
John Girata

Reputation: 2091

Try something like this (the logic seems to work but there might be a typo since I didn't try this on a table with your exact column names):

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

The inner join should create every combination of descriptions, then look for a row that has both a BOGO description and a $5 OFF description.

Upvotes: 1

Lucas
Lucas

Reputation: 14949

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'

Upvotes: 0

Related Questions