質問

I am trying to select products that are found in an range of categories and the product was created within the last 4 months. I have done this with this

select DISTINCT category_skus.sku, products.created_date from category_skus 
LEFT JOIN    products on category_skus.sku = products.sku 
WHERE category_code > ‘70699’ and category_code < ‘70791’ 
and products.created_date > “2013-09-13”;

This is the result:

+------------+------------+
|sku         |created_date|
+------------+------------+
|511-696004PU|2014-01-07  |
+------------+------------+
|291-280     |2013-12-04  |
+------------+------------+
|89-80       |2013-10-07  |
+------------+------------+
|490-1137    |2013-11-21  |
+------------+------------+

However I need to select in multiple ranges within the category_code table. Instead of searching from just '70699' to '70791', I need to also search in '60130' and '60420' (This is not a range, rather additional single categories that are related to the first range of categories).This is what I tried last but I get "Empty set (0.00 sec):

select DISTINCT category_skus.sku, products.created_date from category_skus 
LEFT JOIN products on category_skus.sku = products.sku 
WHERE (category_code BETWEEN ‘70699’ and ‘70791’) 
and WHERE category_code = ‘60130’ and products.created_date > “2013-09-13”;

What am I doing wrong here??? I hope I explained it clear enough and thanks for any help!

役に立ちましたか?

解決

Would

where category_code in (select category_code from category_skus where (category_code       between '70699' and '70791') or (category_code in ('60130','60420')))
and products.created_date > ...

work? Not sure if I understood the question correctly.

他のヒント

Your conditions are a bit mixed up. There won't be any products between 70699-70791 that also = 60130. Try nesting your category_code conditions in their own AND statement:

SELECT DISTINCT category_skus.sku, products.created_date
FROM category_skus 
LEFT JOIN products ON category_skus.sku = products.sku 
WHERE products.created_date > '2013-09-13'
AND (
   category_code BETWEEN '70699' AND '70791'
OR category_code = '60130'
)

There is many way that you can do this. A simple query you can try :-

select x.* from (
select DISTINCT category_skus.sku, products.created_date from category_skus 
LEFT JOIN products on category_skus.sku = products.sku 
WHERE category_code BETWEEN '70699' and '70791' 

union all

select DISTINCT category_skus.sku, products.created_date from category_skus 
LEFT JOIN products on category_skus.sku = products.sku 
WHERE category_code = '60130' or catagory_code='60420'

)x where x.created_date > "2013-09-13"

OR

select DISTINCT s.sku, p.created_date from category_skus s 
LEFT JOIN products p on s.sku = p.sku 
WHERE s.category_code BETWEEN '70699' and '70791' or (s.catagory_code='60130' or s.catagory_code='60420') and 
p.created_date > "2013-09-13"

Hope it will help you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top