A
A
Anton Vertov2018-03-16 11:08:42
MySQL
Anton Vertov, 2018-03-16 11:08:42

How to count same values ​​in multiple columns?

Good day, friends. Can you please tell me how to calculate the same values ​​in several columns?
There is a datanum table, it has columns number1, number2, number3, number4 - they contain digital data.
I need to count the same values ​​in all four columns, how to do it?
To count in one column, I do this:

SELECT number1,COUNT(*) AS total FROM datanumGROUP BY number1 ORDER BY total DESC LIMIT 1

And how to count in all 4 columns at once and summarize?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-03-16
@1Frosty

SELECT `number`, COUNT(*)
  FROM (
    SELECT `number1` AS `number`
      FROM `table`
    UNION ALL SELECT `number2` AS `number`
      FROM `table`
    UNION ALL SELECT `number3` AS `number`
      FROM `table`
    UNION ALL SELECT `number4` AS `number`
      FROM `table`
  ) AS `temp`
  GROUP BY `number`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question