S
S
sr362020-02-11 12:34:35
MySQL
sr36, 2020-02-11 12:34:35

How to display the number of unique records in mysql?

Good afternoon.
There is a table with goods. Each has an article, name, etc.
The task is: to count the number of records in the table, grouped by article and with a unique name.
That is, if we have 2 records in the table, but they have the same name, we need to display 1 in COUNT.
To begin with, I simply made a query in order to get the number of records (not unique)

SELECT artikul, COUNT(*) AS CNT FROM items GROUP BY articul

Further, to uniqueize the records, I added DISTINCT, but this did not give the expected result, the records are not unique by name
SELECT artikul, COUNT(DISTINCT name) AS CNT FROM items GROUP BY articul

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-02-11
@sr36

Now you are asking for the number of unique names for each article.
If you want to get the number of records by unique pairs (article, name), then you need to group by both fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question