0

Background: I have an orders table that contains address columns. I would like to update these with randomly picked addresses taken from a temporary table

Both tables contain address, address1, city and postcode columns

I was thinking the query would be something like:

UPDATE orders (address, address1, city, postcode)
VALUE
(SELECT address, address1, city, postcode
FROM addresses
ORDER BY RAND()
LIMIT 1)

Edit: Note that it needs update all rows with different values.

1
  • Use multiple-table UPDATE syntax. Commented Feb 3, 2023 at 12:37

1 Answer 1

3
UPDATE orders 
JOIN ( SELECT address, address1, city, postcode
       FROM addresses
       ORDER BY RAND()
       LIMIT 1 
       ) AS newdata
SET orders.address = newdata.address,
    orders.address1 = newdata.address1,
    orders.city = newdata.city,
    orders.postcode = newdata.postcode
WHERE orders.id = 72;
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer worked perfectly for one row, however when I remove the where clause at the end all rows are getting the same data. I need each row to be a different random selection from the addresses table.
@Barnaby I help to solve posted task only. If you have another task then create another question.

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.