This sounds a little bit confusing and I don’t know how to put it in words, but I’m having a hard time finding a solution of this problem.
I want to ‘group by’ the rows and count them in the table using the ‘digit’ column that has the same digits regardless of the digit’s position.
example:
this is the table
Id | Name | Digit |
---|---|---|
1 | name1 | 123 |
2 | name2 | 213 |
3 | name3 | 132 |
4 | name4 | 122 |
5 | name5 | 456 |
the answer would be: with count()
Name | Digit | Count |
---|---|---|
name1 | 123 | 3 |
name4 | 122 | 1 |
name5 | 456 | 1 |
additional details:
- the digit column is numeric
- length fixed(3)
- the ‘name’ column display must be any from the group (could be name1, name2, or name3)
mysql version is 82 from cpanel.
3
Answers
You can think the single digits of the
Digit
column as coordinates in a 3D space. If the coordinates are swapped the resulting vector will have the same lenght. You can use this property to create an efficient query:I don’t calculate the square root because we are not interested in the exact length. I named the column
c
instead ofcount
becauseCOUNT
is reserved word in SQL.