Naomi,
Thats statement could be very aggresive and update recordsets that has the same values. I am refering to the statement in the thread at stackoverflow. The join has to be joined over an unique identifier (field) and not the value field…
Thats the only difference between my inner join and the one at stackoverflow.
A table should have a field with a primary key (unique). For example if you have a table named person, it would for example contain these fields: id, name, surname, telephone, state, age
“id” could be an integer that the database keeps track of, and automatically increases for every new record. Nothing you have to keep track of. This “id” is the key to everything and this is the field you always should look up your data against. Not the other fields.
Update Person set name = ‘kim’ where state = ‘NY’ - will affect all rows with the state of NY.
update Person set name = ‘kim’ where id = 1 - will affect this row only.
So for my sample with the innerjoin and the above layout of the table would be:
--
UPDATE Person t1 -- This t1 is the alias for the first table we are joining
INNER JOIN (SELECT min(age) FROM Person) t2 --this would be the second alias for the same table and we are selecting the person with the lowest age. Alias is t2.
on t1.id = t2.id --this would tell the query that the id has to be same on both rows, since it is unique.
SET t1.age = 25 --Update the age in the first table alias we created.
--
The query above will update the person in the table that is the youngest and set hes age to 25.
Joakim
[import]uid: 81188 topic_id: 32713 reply_id: 130071[/import]