我正在尝试从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
是的我有语法错误,但我不确定如何解决它
总结我想选择一个随机的call_codes.call_code_id并将它分配给phone_calls.call_code_id
答案 0 :(得分: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
这会将所有call_code_id
和所有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;
(不知道是否可以只使用一个查询来完成它)。它可能很慢,但应该有效。