0

我正在尝试从 select 语句中更新字段。这是我当前的查询

UPDATE phone_cals
SET call_code_id = id, result_code_id = 0, call_subject = title WHERE status = 1
select call_code_id AS id, call_code_title AS title FROM call_codes

我正在尝试设置

phone_calls.call_code_id = call_codes.call_code_id 
phone_calls.result_code_id = 0
phone_calls.call_subject = call_codes.call_code_title 

WHERE phone_calls.status = 1

是的,我有一个语法错误,但我不知道如何修复它

Summery 我想选择一个随机的 call_codes.call_code_id 并将其分配给 phone_calls.call_code_id

4

1 回答 1

0

如果我正确理解你的问题,我认为你需要这样的东西:

UPDATE
  phone_calls,
  (select call_code_id, call_code_title FROM call_codes ORDER BY rand() LIMIT 1) c2
SET
  phone_calls.call_code_id = c2.call_code_id,
  phone_calls.result_code_id = 0,
  phone_calls.call_subject = c2.call_code_title
WHERE
  phone_calls.call_subject = 1

这会将 allcall_code_id和 all更新call_subject为从call_codes表中选择的随机值,其中 call_subject=1。

编辑

如果您需要将每一行更新为不同的随机值,我认为您可以使用它:

UPDATE
  phone_calls
SET
  call_code_id = (select call_code_id FROM call_codes ORDER BY rand() LIMIT 1),
  result_code_id = 0
WHERE
  phone_calls.call_subject = 1;

UPDATE
  phone_calls INNER JOIN call_codes
  ON phone_calls.call_code_id = call_codes.call_code_id
SET
  phone_calls.call_subject = call_codes.call_code_title
WHERE
  phone_calls.call_subject = 1;

(不知道是否可以仅使用一个查询来完成)。它可能很慢,但它应该可以工作。

于 2013-04-03T18:53:33.360 回答