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?
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
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
).
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_name
s.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.