1

I have mysql database structure and values:

name  | x | y | z
------------------
name1 | 1 | 1 | 2
name2 | 2 | 1 | 3 
name3 | 1 | 1 | 1

And now i need add extra column with counter unique values (x,y,z) for each row:

name  | x | y | z | unique
--------------------------
name1 | 1 | 1 | 2 | 2
name2 | 2 | 1 | 3 | 3
name3 | 1 | 1 | 1 | 1

How can i do that?

kuguary
  • 11
  • 1
  • 1

2 Answers2

1

you can use select count distinct

SELECT *,COUNT (DISTINCT column-name)  FROM table-name;

more answers here > Counting DISTINCT over multiple columns, its already been answered.

Community
  • 1
  • 1
Freak
  • 115
  • 1
  • 1
  • 16
0

If you don't need it to scale massively you can do this

SELECT *, 
(CASE 
 WHEN x=y=z THEN 1
 WHEN x=y OR x=z OR y=z THEN 2
 ELSE 3
END) as `unique`
FROM `table`
Matt
  • 1,749
  • 2
  • 12
  • 26