我有两张桌子,organization
张和address
张.
organization
表有address_id
列,但所有行的address_id
都设置为NULL.
我想将组织表中的address_id
设置为地址表中的随机ID,但我也希望所有ID都是唯一的.
这就是我try 过的:
UPDATE organization
SET address_id = (SELECT id
FROM address
WHERE id NOT IN (SELECT address_id FROM organization WHERE address_id IS NOT NULL)
LIMIT 1)
但是,子查询不会在每次更新后获得更新值.
What I'm expecting/trying to do:
Assuming on the first update sets address_id
to 6, then on the next update 6 should not be returned since 1 organization already has the address_id
6.
But all rows in organization end up having the same address_id
.
How can I go about this?