1

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;

4 Answers 4

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Values now work well, but count is now 1 (1 for every unique value). Should i just sum all of them using php or is there a mysql alternative that returns the final count?
0

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.

Comments

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

Comments

0

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;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.