MS SQL查询选择不同的product_id

时间:2013-06-06 12:00:04

标签: sql-server-2005 distinct

SELECT distinct fp.fcast_product_id, fp.id, fp.customer_id, pm.product_name FROM 
f_product AS fp LEFT JOIN product_master AS pm ON fp.customer_id=pm.customer_id 
WHERE fp.customer_id = '10004'

我得到了这个答案

710070  934 10004   EUREKA
710070  934 10004   EXCELLAY
711080  2933    10004   EUREKA
711080  2933    10004   EXCELLAY

但我想要这样

710070  934 10004   EUREKA
711080  2933    10004   EXCELLAY

示例表结构

表1:f_product

id  fcast_product_id  customer_id

14  710010          24018
15  710010          24018
79  711080          28006
80  711080          39963
81  711080          39963
82  711080          10004
594 710020          12238
595 710020          12238
596 710020          14004
597 710020          14004
598 710020          30112
599 710020          30112
600 710020          39043
601 710020          40173
934 710070          10004

表2:product_master

id  product_id  customer_id   product_name

24  710010      10082       test8
25  711020      11006       test9
26  711020      11006       test9
27  710010      11026       test8
28  710010      11029       test8
29  711020      11029       test9
35  710070      10004       EUREKA
36  711080      10004       EXCELLAY
53  710070      12242       EUREKA
59  710070      12347       EUREKA
80  710070      19023       EUREKA
21  711080      10077       EXCELLAY
33  711080      11030       EXCELLAY
62  711080      14037       EXCELLAY

1 个答案:

答案 0 :(得分:0)

你确定要分明吗?

试试这个:

SELECT fp.fcast_product_id, fp.id, fp.customer_id, pm.product_name 
FROM 
f_product AS fp 
LEFT JOIN product_master AS pm 
ON fp.customer_id=pm.customer_id 
and pm.product_id =fcast_product_id
WHERE fp.customer_id = '10004'

B.t.w。你的例子中没有id 2933