Using DISTINCT and COUNT together in a MySQL Query

前端 未结 7 866
旧巷少年郎
旧巷少年郎 2020-11-30 22:04

Is something like this possible:

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

What I want is to get the number of unique pro

相关标签:
7条回答
  • 2020-11-30 22:30

    You were close :-)

    select count(distinct productId) from table_name where keyword='$keyword'
    
    0 讨论(0)
  • 2020-11-30 22:34

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

    SELECT COUNT(*) FROM t1 GROUP BY keywork;
    
    0 讨论(0)
  • 2020-11-30 22:35

    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'
    
    0 讨论(0)
  • 2020-11-30 22:40

    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; 
    
    0 讨论(0)
  • 2020-11-30 22:41

    use

    SELECT COUNT(DISTINCT productId) from  table_name WHERE keyword='$keyword'
    
    0 讨论(0)
  • 2020-11-30 22:54

    SELECTING DISTINCT PRODUCT AND DISPLAY COUNT PER PRODUCT

    for another answer about this type of question this is my another answer for getting count of product base on product name distinct like this sample below:

    Table Value

    select * FROM Product
    

    Counted Product Name

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

    Record Count: 4; Execution Time: 2ms

    0 讨论(0)
提交回复
热议问题