Counting number of grouped rows in mysql

后端 未结 5 691
有刺的猬
有刺的猬 2021-01-31 14:22

In a table xyz I have a row called components and a labref row which has labref number as shown here

Table xyz

labref             component
NDQA201303001         


        
5条回答
  •  长发绾君心
    2021-01-31 14:33

    Why not use num_rows.

    If you do it using this method, You don't have to modify the query in any way.

    if ($result = $mysqli->query("SELECT DISTINCT component, COUNT( component ) 
        FROM `xyz`
        WHERE labref = 'NDQA201303001'
        GROUP BY component")){
    
        /* determine number of rows result set */
        $row_cnt = $result->num_rows;
    
        printf("Result set has %d rows.\n", $row_cnt);
    
        /* close result set */
        $result->close();
    }
    

提交回复
热议问题