0

I wanted to update MySQL table with certain rules. I have let's say 3 column: ID, Name, Checked and I want to update Name to all rows equally where Checked = 0. In another table I have list 'Names` from where I pull my names.

Is this possible to do with one UPDATE query, or do i need to use some loop? Will there be a problem if let's say I have 11 rows to update but i have 10 names, how the query update those fields.

rtstorm
  • 339
  • 1
  • 4
  • 16
  • yes that could happen, update table based on condition with using set on another table. you could search here on SO https://stackoverflow.com/questions/11709043/mysql-update-column-with-value-from-another-table – Kevin Jun 27 '18 at 09:41
  • The Name field consists of single value or multiple values ? It'll be better if you could post the Db table structures to gain better clarity. – Kishen Nagaraju Jun 27 '18 at 09:41
  • The `Name` field is names of employees. List of active employees is listed in another table. So every once in a month I want to update the remaining unchecked row with new employees and redistribute equally. – rtstorm Jun 27 '18 at 09:44

1 Answers1

0

I find solution:

UPDATE `table` SET `ID` = (select `ID` from `name` ORDER BY rand() LIMIT 1) WHERE `Lock_ID` = 0

This query will randomly use name from other table and update it (it is 99% correct solution) it will redistribute name's almost the same for every row.

rtstorm
  • 339
  • 1
  • 4
  • 16