从SQL查询获取数据,其中另一列是唯一的

时间:2014-06-12 08:47:16

标签: mysql sql database

有人可以帮我解决这个我想写的问题。

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;

这个查询工作正常,但我也想只选择sk_,其中search_prod_id是不同的。我认为这样可行,但事实并非如此:

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;

当我运行时,我可以看到每个sku的search_prod_id是相同的。

有人可以告诉我我做错了吗?

5 个答案:

答案 0 :(得分:2)

您可以通过对search_prod_id进行分组来强制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;

sku in (SELECT distinct search_prod_id from product p ) 

同样在你的查询中你正在检查sku对search_prod_id,每次匹配它只是运气(运气不好IMO)

{{1}}

答案 1 :(得分:0)

尝试此查询可能有效,

'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;'

答案 2 :(得分:0)

不完全确定我的语法是否正确,但是类似的东西。我在最后一个AND子句中使用了GROUPING,只给了只有一个的search_prod_id!

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;

答案 3 :(得分:0)

您的查询存在的问题是,在执行

sku in (SELECT distinct search_prod_id from product p ) 

您要添加的查询sku值等于search_prod_id。 我认为您正在寻找的查询是这样的:

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;

答案 4 :(得分:0)

在最后一个语句(where .. and sku in (SELECT distinct search_prod_id ...中,您只选择与search_prod_id相等的sku。我想你需要像where ... and search_prod_id in (SELECT distinct search_prod_id from product ...

这样的东西

我认为这应该有效:

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;