Question: I have a table with several columns. The only way to tell the connection to each row is by session\_id   

My goal is to update the age column with a random number.   

Therefore I used following query

```
UPDATE userdata
SET age = (FLOOR(18 + RAND() * 62))
WHERE session_id IN 
(
    SELECT session_id FROM
    (
        SELECT DISTINCT session_id
        FROM userdata
    ) AS temp
)

```

This works fine but every distinct session\_id still has multiple rows and the age has different values.I know this query does what it should do but how can I change my query to get unique age values for each row with the same session\_id?

e.g. 

```
session_id  |  age
1              25
2              35
2              35
3              51
3              51
3              51

```

Thank you in advance and please be nice, since this is my first stackoverflow question.

Answer: 
Try this:

```
update userdata
inner join (select *, (FLOOR(18 + RAND() * 62)) as age2 from userdata group by session_id) T ON T.session_id = userdata.session_id
set userdata.age = T.age2

```
This seems the wrong way around. It seems session\_id is not unique (when perhaps it should be?--which is where the first way you can correct this is)... so you are trying to create a unique column based on it...but as you rightly point out you can't create a new number based on the session id as it too will be the same. Most obvious solution, add an auto-increment column:

ALTER TABLE yourtablename add Age int(11) PRIMARY KEY AUTO\_INCREMENT;