0

I'm trying to figure out why i cannot run a multiple AND conditions in mysql query.

basically this is my code:

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' AND category_name='Boys Clothes' GROUP BY category_name

the code above doesn't return anything but when I run the code individually like this:

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' GROUP BY category_name

or like this:

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Boys Clothes' GROUP BY category_name

then it works fine!

This is such a basic issue and unfortunately i cannot figure out how to resolve it.

could someone please advise on this issue?

3
  • Girls clothes OR Boys clothes? Commented Nov 15, 2015 at 0:07
  • @H.HISTORY In that case, we need more information. Could you give some sample output of your 2 working queries? Commented Nov 15, 2015 at 0:10
  • You should describe what you want to accomplish. Sample data and desired results are quite helpful. Commented Nov 15, 2015 at 0:12

3 Answers 3

2

It's because the logic of the query is off. If you think about it, you can't have a category name where its name is Girls Clothes AND Boys Clothes. However, you can have a result where the name is Girls Clothes OR Boys Clothes.

The query should look like:

SELECT id, 
category_name, 
url, 
category_name_unedit 
FROM mytable 
WHERE category_name='Girls Clothes' 
OR category_name='Boys Clothes' 
GROUP BY category_name
Sign up to request clarification or add additional context in comments.

Comments

2

I'm guessing that you want urls that are in both categories. If so:

SELECT url
FROM mytable 
WHERE category_name IN ('Girls Clothes', 'Boys Clothes')
GROUP BY url
HAVING COUNT(*) = 2;

If you just want to check for either category, then use IN in your query (or OR).

Comments

1
SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' AND category_name='Boys Clothes' GROUP BY category_name

Because it is not possible for one item to have 2 category_names.

What you probably intend is to find it if it is EITHER "Girls Clothes" or "Boys Clothes". In that case, you would use OR instead of AND.

SELECT id, category_name, url, category_name_unedit FROM mytable WHERE category_name='Girls Clothes' OR category_name='Boys Clothes' GROUP BY category_name

1 Comment

Ugh. Need to check out first, second and third normal forms for database normalization / design. Hint. Boys clothes. Girls clothes. Unisex kids clothes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.