Jason Bourne
Jason Bourne

Reputation: 11

Count 3 rows Mysql

Please help, I run query :

SELECT * 
FROM  `euclidean` 
WHERE id_latihan =  '46'
ORDER BY jarak ASC 
LIMIT 3

Output :

enter image description here

I want to know how to count label from that output, I want the output showing like this :

   +-------+---+
   | LOBBY | 2 |
   +-------+---+
   | PMB   | 1 |
   +-------+---+

Any help will appreciate. Thanks in advance

Upvotes: 0

Views: 48

Answers (3)

Gagan V
Gagan V

Reputation: 126

SELECT label, count(*) FROM euclidean WHERE id_latihan = '46' GROUP BY label ORDER BY label LIMIT 3

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133400

assuming you want the resulting count from your select you could use

select label, count(*)
from (
    SELECT * 
    FROM  `euclidean` 
    WHERE id_latihan =  '46'
    ORDER BY jarak ASC 
    LIMIT 3) t 
group by label 

or directly form you query

    SELECT label, count(*)
    FROM  `euclidean` 
    WHERE id_latihan =  '46'
    GROUP BY label
    ORDER BY label

Upvotes: 0

Shivrudra
Shivrudra

Reputation: 684

Your question is similar to Join 3 tables with Count . Try this sql. This may be resolve your problem.

    SELECT 
        p.label,
        Count( t.id_latihan ) AS id_latihan 
    FROM 
        euclidean p 
        LEFT JOIN euclidean t ON p.id_latihan = t.id_latihan
    where 
        p.id_latihan = 46
    GROUP BY 
        p.label

Upvotes: 0

Related Questions