1

I tried using AND for the same column in WHERE clause, but it is not giving me any output. But there are two Customer names in the data set which satisfies the given condition. Where have I gone wrong?

SELECT CONCAT(c.CustFirstName," ",c.CustLastName) AS CustomerName
FROM Musical_Preferences mp
JOIN Customers c ON c.CustomerID = mp.CustomerID 
WHERE mp.StyleID = 15 AND mp.StyleID = 24; 

This above query worked fine when I put OR for same column in WHERE clause, but why not for AND?

My data:

CustomerID  CustFirstName   CustLastName    CustStreetAddress       CustCity    CustState   CustZipCode CustPhoneNumber CustomerID  StyleID PreferenceSeq
10001       Doris           Hartwig         4726 - 11th Ave. N.E.   Seattle     WA          98105       555-2671        10001       10      2
10001       Doris           Hartwig         4726 - 11th Ave. N.E.   Seattle     WA          98105       555-2671        10001       22      1

More data in image

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Sree
  • 973
  • 2
  • 14
  • 32
  • 1
    This should work. You defined the exact same conditions `mp.StyleID = 24 and mp.StyleID = 24`. Can you show some example data? – Sebastian Brosch May 18 '18 at 21:12
  • Im voting to close as 'beyond daft' – Strawberry May 18 '18 at 21:18
  • *"looks like MySQL workbench is only considering the first condition"*: what makes you say so? What data you have in your table, what output did you expect, what do you get instead? – trincot May 18 '18 at 21:20
  • CustomerID CustFirstName CustLastName CustStreetAddress CustCity CustState CustZipCode CustPhoneNumber CustomerID StyleID PreferenceSeq 10001 Doris Hartwig 4726 - 11th Ave. N.E. Seattle WA 98105 555-2671 10001 10 2 10001 Doris Hartwig 4726 - 11th Ave. N.E. Seattle WA 98105 555-2671 10001 22 1 – Sree May 18 '18 at 21:23
  • The data is in the above form for the two tables involved in the query. I am supposed to get two names but I am getting none. But when i debug i found out that there are two names for the specified condition. – Sree May 18 '18 at 21:25
  • @trincot Yeah I misunderstood the output. I am editing it. I am not receiving any output values but I am supposed to get two names for the specified condition. – Sree May 18 '18 at 21:26
  • 1
    @Sree can you put that data in the question rather than a comment? you cannot format it in a comment – Mr.Mindor May 18 '18 at 21:32
  • @Mr.Mindor Done, added an image link with data set. – Sree May 18 '18 at 21:41
  • Am I blind? I don't see any evidence of records with a StyleID of 24. The only place I even see 24 at is in the middle of a couple copies of a phone number, – Uueerdo May 18 '18 at 21:44
  • I commented this in reply to an answer, but I'll restate it here. Sree seems to be wanting an answer to the question "How do I get a list of customer names who each like BOTH style 15 and style 24" (not a list of customers that like either one. – Shadow Radiance May 18 '18 at 21:58

3 Answers3

4

The condition StyleID = 15 AND StyleID = 24 can never return anything because it can never be true. So what you're getting is expected. If you want to get the customers that have both StyleID 15 and 24 (in different records), then you need to group the records by customer:

SELECT CONCAT(c.CustFirstName," ",c.CustLastName) AS CustomerName
FROM Musical_Preferences mp
JOIN Customers c ON c.CustomerID = mp.CustomerID 
WHERE mp.StyleID IN(15, 24)
GROUP BY c.CustomerID, c.CustFirstName, c.CustLastName
HAVING COUNT(mp.CustomerID) = 2;

I added c.CustFirstName and c.CustLastName to the GROUP BY. Alternatively, you can only group by the ID and then use MIN() or MAX() on the first and last name. Both ways are more or less then same.

This query will only work if your data can never have a customer with the same StyleID more than once (example: customer 1001 has 3 records with StyleID 15, 24, and 24).

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
1

WHERE conditions operate on rows created by the FROM clause, not across rows if you try WHERE somefield = 1 AND somefield = 2 you will never get results, a field cannot have two values at the same time.

I think what you are looking for is

SELECT ...
FROM ...
WHERE somefield IN (15, 24)
GROUP BY ...
HAVING COUNT(DISINCT somefield ) = 2
;
Uueerdo
  • 15,723
  • 1
  • 16
  • 21
0

If your query with a single condition does not return any row, your query won't return any row.

Please try this:

SELECT CONCAT(c.CustFirstName," ",c.CustLastName) AS CustomerName
FROM Musical_Preferences mp
JOIN Customers c ON c.CustomerID = mp.CustomerID 
WHERE mp.StyleID = 24;

Does it return anything?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • No, It gives results without WHERE condition as well. I tried it and it gave me all the CustomerNames a total of 36 rows. – Sree May 18 '18 at 21:36
  • What about a **single** `WHERE` condition? – The Impaler May 18 '18 at 21:36
  • When I tried with only 'where mp.StyleID = 24' it gives 2 results which is correct and Similarly when I tried with 'where mp.StyleID = 15' it gives 3 results which is again correct. For StyleID = 15 and StyleID = 24 there are two customers and I am supposed to get those two names but I am getting no results. – Sree May 18 '18 at 21:40
  • I kind of give up. This is such a simple case that -- if true -- deserves to be filed as a MySQL bug. What MySQL version are you using? – The Impaler May 18 '18 at 21:42
  • And how on earth do you expect `StyleID = 15 and StyleID = 24` to give you any results? You probably missed some logic classes at school. That condition means that `StyleID` must be 15 and 24 at the same time in the same record , how can that ever happen? – Racil Hilan May 18 '18 at 21:44
  • *I kind of give up. This is such a simple case, that if true, deserves to be filed as a bug of MySQL*. Wow, it seems you probably missed some logic classes at school too. – Racil Hilan May 18 '18 at 21:45
  • I'm sorry, I thought he was talking about two separate cases: 15, and 24. Isn't it the case? – The Impaler May 18 '18 at 21:46
  • *I thought he was talking about two separate cases: 15, and 24*, read the condition again `StyleID = 15 and StyleID = 24`. He already told you what he gets for the two separate cases, 2 for `StyleID = 24` and 3 for `StyleID = 15`. – Racil Hilan May 18 '18 at 21:48
  • I think what @Sree is trying to get is "those customers who like BOTH style 15 and 24" – Shadow Radiance May 18 '18 at 21:49
  • @ShadowRadiance Yes, exactly. And he successfully tried something like `StyleID = 15 OR StyleID = 24`, but he somehow seems to expect some result from `StyleID = 15 AND StyleID = 24`. Obviously, he's thinking of it in English, not math/logic. – Racil Hilan May 18 '18 at 21:51
  • @RacilHilan The OR won't work as written; it returns all the customers that like EITHER style 15 or 24 - he wants the list of customers that like BOTH – Shadow Radiance May 18 '18 at 21:52
  • Can you maybe help with "what query would I execute to get a list of customer names that like both styles?" – Shadow Radiance May 18 '18 at 21:54
  • 1
    Sorry. I was unclear - my meaning was "I think the OP wants a thing he didn't explicitly ask for" - he is expecting the WHERE-AND to return "folks who like Jazz and Rock", since his WHERE-OR did (happen to) return "folks who like Jazz or Rock". Perhaps he should open another question, rather than fixing this one... – Shadow Radiance May 18 '18 at 22:07