Can somebody help me with this query I am trying to write.
SELECT sku FROM product p where sku
not in(SELECT sku FROM price_adjustment p) and sku
in (SELECT sku FROM searchable_product s where is_visible = 'T') and sku
in (SELECT sku FROM channel_pricing c
where offer_price > 10.00 and offer_price % 2 = 0) limit 5;
This query works fine but I also want to only pick skus where the search_prod_id is distinct. I thought this would work but it doesn't:
SELECT sku, search_prod_id FROM product p where sku
not in(SELECT sku FROM price_adjustment p) and sku
in (SELECT sku FROM searchable_product s
where is_visible = 'T') and sku in
(SELECT sku FROM channel_pricing c
where offer_price > 10.00 and offer_price % 2 = 0) and sku
in (SELECT distinct search_prod_id from product p ) limit 5;
When I run that I can see the search_prod_id is the same for each sku.
Can somebody tell me what I'm doing wrong?
You can force the search_prod_id
to be different by grouping them
SELECT MAX(sku) sku, search_prod_id
FROM product p
WHERE sku NOT IN(SELECT sku
FROM price_adjustment)
AND sku IN (SELECT sku
FROM searchable_product
WHERE is_visible = 'T')
AND sku IN (SELECT sku
FROM channel_pricing c
WHERE offer_price > 10.00
AND offer_price % 2 = 0)
GROUP BY search_prod_id
LIMIT 5;
also in your query you're checking sku against search_prod_id, every match it's just luck (bad luck IMO)
sku in (SELECT distinct search_prod_id from product p )
Try this Query it may work,
'SELECT sku FROM product p where search_prod_id in(
SELECT DISTINCT search_prod_id ,sku FROM product p where sku not in(
SELECT sku FROM price_adjustment p) and sku in (
SELECT sku FROM searchable_product s
where is_visible = 'T') and sku in
(SELECT sku FROM channel_pricing c
where offer_price > 10.00 and offer_price % 2 = 0) limit 5;'
Not entirely sure I have the syntax correct, but something similar. I used a GROUPING in the last AND clause to only give search_prod_id s where there is only one!
SELECT sku, search_prod_id
FROM product p
WHERE sku not in(SELECT sku FROM price_adjustment p)
and sku in (SELECT sku FROM searchable_product s where is_visible = 'T' )
and sku in (SELECT sku FROM channel_pricing c where offer_price > 10.00 and offer_price % 2 = 0)
and sku in (SELECT search_prod_id from product p GROUP BY search_prod_id HAVING count(*) = 1 ) limit 5;
The problem with your query is that when doing
sku in (SELECT distinct search_prod_id from product p )
you are adding to the query sku values which are equal to search_prod_id. I think that the query you are looking for is this one:
SELECT distinct sku, search_prod_id FROM product p where sku not in(SELECT sku FROM price_adjustment p) and sku in (SELECT sku FROM searchable_product s where is_visible = 'T') and sku in (SELECT sku FROM channel_pricing c where offer_price > 10.00 and offer_price % 2 = 0) limit 5;
In the last statement (where .. and sku in (SELECT distinct search_prod_id ...
you select only the sku's which are equal to the search_prod_id. I think you will need something like where ... and search_prod_id in (SELECT distinct search_prod_id from product ...
I think this should work:
SELECT sku, search_prod_id FROM product p where sku
not in(SELECT sku FROM price_adjustment p) and sku
in (SELECT sku FROM searchable_product s
where is_visible = 'T') and sku in
(SELECT sku FROM channel_pricing c
where offer_price > 10.00 and offer_price % 2 = 0) and search_prod_id
in (SELECT distinct search_prod_id from product p ) limit 5;
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.