Answers:
用
SELECT COUNT(DISTINCT productId) from table_name WHERE keyword='$keyword'
SELECT COUNT(DISTINCT(productId)) from table_name WHERE keyword='$keyword'
。我更喜欢您的版本将两个参数传递给COUNT( )
。
你很近:-)
select count(distinct productId) from table_name where keyword='$keyword'
仅供参考,这可能更快
SELECT count(1) FROM (SELECT distinct productId WHERE keyword = '$keyword') temp
比这个,
SELECT COUNT(DISTINCT productId) WHERE keyword='$keyword'
SELECT count(*) FROM (SELECT distinct productId WHERE keyword = '$keyword') temp
对于这种类型问题的另一个答案,这是我根据产品名称区分产品数量的另一个答案,如下例所示:
select * FROM Product
SELECT DISTINCT(Product_Name),
(SELECT COUNT(Product_Name)
from Product WHERE Product_Name = Prod.Product_Name)
as `Product_Count`
from Product as Prod
记录数:4;执行时间:2ms