Biker John
Biker John

Reputation: 2711

MySQL: Print counted values

Table:

+-------------+
| izvajalecID |
+-------------+
|          20 |
|          21 |
|          21 |
|          20 |
|          21 |
+-------------+

I would like to count all the unique ID's and print their values.

For example:

Unique ID's: 2 Values: 20, 21

I tried with the following query. Count works fine, but it returns only one(first) value. What am i doing wrong?

SELECT COUNT(distinct izvajalecID), s.izvajalecID FROM (SELECT izvajalecID FROM servis) s;

Upvotes: 1

Views: 71

Answers (4)

potashin
potashin

Reputation: 44581

SELECT 
    izvajalecID
FROM 
    servis 
GROUP BY
    izvajalecID

UNION

SELECT 
    COUNT(DISTINCT izvajalecID)
FROM 
    servis 

Fiddle

The last value in the set is for COUNT of unique values. You can also change them places and it will be the first value, just as you wish.

Upvotes: 2

fluminis
fluminis

Reputation: 4039

If you want the count of the uniq ids and get the list of the uniq Ids you need two queries.

One to count

SELECT COUNT(distinct izvajalecID) FROM servis GROUP BY izvajalecID;

And one to get the list.

SELECT distinct izvajalecID FROM servis GROUP BY izvajalecID;

Upvotes: 0

Gooner
Gooner

Reputation: 256

SELECT COUNT(DISTINCT izvajalecID) AS COUNT, GROUP_CONCAT(DISTINCT izvajalecID) AS ID
FROM servis

Upvotes: 0

Raging Bull
Raging Bull

Reputation: 18737

Try this:

SELECT izvajalecID, COUNT(DISTINCT izvajalecID) as COUNT
FROM servis
GROUP BY izvajalecID

Result:

IZVAJALECID   COUNT
20            1
21            1

See result in SQL Fiddle.

Upvotes: 0

Related Questions