142

Is something like this possible:

SELECT DISTINCT COUNT(productId) WHERE keyword='$keyword'

What I want is to get the number of unique product Ids which are associated with a keyword. The same product may be associated twice with a keyword, or more, but i would like only 1 time to be counted per product ID

7 Answers 7

312

use

SELECT COUNT(DISTINCT productId) from  table_name WHERE keyword='$keyword'
4
  • Updated the answer as it is close to become a great answer and it was syntactically incorrect. Commented Sep 8, 2014 at 10:20
  • Im against answers that don't offer optimal performance when it comes to databases. It's critical to keep perfomance standards. I'd go with @alistair-hart 's answer.
    – JDuarteDJ
    Commented Jan 20, 2015 at 15:45
  • 1
    Stumbled across this while looking for something else and learned something. I've always done this with SELECT COUNT(DISTINCT(productId)) from table_name WHERE keyword='$keyword'. I like your version better passing two params to COUNT( ). Commented Aug 1, 2018 at 18:23
  • To my surprise, I found that there cannot be a space between "COUNT" and the opening parenthesis - at least in version 10.1.41 of MariaDB.
    – DRosenfeld
    Commented Jan 9, 2020 at 8:19
73

I would do something like this:

Select count(*), productid
from products
where keyword = '$keyword'
group by productid

that will give you a list like

count(*)    productid  
----------------------
 5           12345   
 3           93884   
 9           93493    

This allows you to see how many of each distinct productid ID is associated with the keyword.

0
42

You were close :-)

select count(distinct productId) from table_name where keyword='$keyword'
1
  • Updated the answer as it is close to become a good answer and it was syntactically incorrect. Commented Sep 8, 2014 at 10:20
25

FYI, this is probably faster,

SELECT count(1) FROM (SELECT distinct productId WHERE keyword = '$keyword') temp

than this,

SELECT COUNT(DISTINCT productId) WHERE keyword='$keyword'
2
  • Not sure if it's faster, but the way to count multiple columns with distinct keyword
    – VladL
    Commented May 9, 2014 at 9:08
  • 1
    Fantastic answer. His answer is at least 100 times more faster in my case. A slight alteration for understanding @Alistair's code is SELECT count(*) FROM (SELECT distinct productId WHERE keyword = '$keyword') temp
    – KarthikS
    Commented Mar 20, 2017 at 6:41
9

What the hell of all this work anthers

it's too simple

if you want a list of how much productId in each keyword here it's the code

SELECT count(productId),  keyword  FROM `Table_name` GROUP BY keyword; 
1
  • Thanks for that comment, not the purist answer to his exact question, but maybe what he's looking for, and anyway useful.
    – Leo
    Commented Sep 2, 2013 at 10:29
7

SELECTING DISTINCT PRODUCT AND DISPLAY COUNT PER PRODUCT

for another answer about this type of question, this is my way of getting the count of product based on their product name using distinct. Here a sample:

List of Product (SQLFiddle):

select * FROM Product

Product Name Count Using Distinct (SQLFiddle):

SELECT DISTINCT(Product_Name),
(SELECT COUNT(Product_Name) 
from Product  WHERE Product_Name = Prod.Product_Name)  
as `Product_Count`
from Product as Prod

Optimized version without Distinct:

SELECT Product_Name, COUNT(Product_Name) AS `Product_Count`
FROM Product
GROUP BY Product_Name
-6

Isn't it better with a group by? Something like:

SELECT COUNT(*) FROM t1 GROUP BY keywork;
1
  • 1
    He wants the number of distinct productID's. Your query returns the number of rows for each keyword.
    – David
    Commented Jun 16, 2009 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.