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?

2 Answers 2

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.

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

1 Comment

did this answer help?
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`

2 Comments

Unfortunately i have more columns and several millions of rows. Any other idea?
@kuguary are you using this in conjunction with php or just direct sql queries?

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.