在MySQL中我有四个表:
报价
id | uid | tid | message | time
quotes_rnd
id | message
用户
idu | username
作者
id | uid | name
我需要填充quotes
表,这是我的程序:
BEGIN
INSERT INTO quotes
(message)
SELECT message
FROM quotes_rnd ORDER BY RAND() limit 1;
UPDATE quotes SET tid = (
SELECT id FROM authors WHERE id>10 ORDER BY rand() LIMIT 1)
WHERE tid = 0;
UPDATE quotes SET uid = (
SELECT idu FROM users WHERE idu BETWEEN 13 AND 63 ORDER BY rand() LIMIT 1)
WHERE uid = 0;
END
问题
UPDATE quotes SET uid = (
SELECT idu FROM users WHERE idu BETWEEN 13 AND 63 ORDER BY rand() LIMIT 1)
WHERE uid = 0;
在此查询行之前,tid
值已设置,而不是uid=0
。
如果SET uid
仅在表uid.tid
中所有行的quotes
与此处最后一行的随机行不同时才会$('#divcontainer').droppable({ //make icon image droppable.
drop: function(event, ui)
{
$('#droppable_video_cotainer').removeClass('col-lg-12 col-md-12 col-sm-12 col-xs-12').addClass('col-lg-7 col-md-7 col-sm-7 col-xs-12');
var id = ui.draggable.attr("id");
if(id == 'multiple_choice_question')
{
var div= '<div class="box box-mintgreen box-solid video-edit">';
div+='<div class="box-header with-border">';
div+= '<h3 class="box-title"><i class="fa fa-question-circle"></i> Quiz</h3>';
div+='<div class="box-tools pull-right">';
div+='<button class="btn btn-box-tool clickable" data-widget="remove" data-effect="fadeOut"><i class="fa fa-times"></i></button>';
div+='</div></div>';
div+='<div class="box-body">';
div+='<textarea style="width: 410px;" ng-model="videoCourseQuestions.mcss_question" rows="5"></textarea>';
div+='<a id="mcss_add_option" align="center" style="cursor:pointer"> +Add option</a></br>';
div+='<div id="addRdo" class="quiz-ans"> ';
div+='<ul class="mcss_option_list" id="mcss_option_list">';
div+='<li><input type="radio" id="mcq_option1" ng-model="videoCourseQuestions.mcss_option" ng-value="option1"><input type="text" id="mcq_option#1" class="option" ng-model="videoCourseQuestions.mcss_option_1" placeholder="Enter your Option 1"/></li>';
div+='<li><input type="radio" id="mcq_option2" ng-model="videoCourseQuestions.mcss_option" ng-value="option2"><input type="text" id="mcq_option#2" class="option" ng-model="videoCourseQuestions.mcss_option_2" placeholder="Enter your option 2"/></li>';
div+='<li><input type="radio" id="mcq_option3" ng-model="videoCourseQuestions.mcss_option" ng-value="option3"><input type="text" id="mcq_option#3" class="option" ng-model="videoCourseQuestions.mcss_option_3" placeholder="Enter your option 3"/></li>';
div+='<li><input type="radio" id="mcq_option4" ng-model="videoCourseQuestions.mcss_option" ng-value="option4"><input type="text" id="mcq_option#4" class="option" ng-model="videoCourseQuestions.mcss_option_4" placeholder="Enter your option 4"/></li>';
div+='</ul>';
div+='</div>';
div+='<p class="sub"><input type="button" id="mscc_question_submit" class="btn btn-danger" set-on-click value="Submit"> <input type="reset" ng-model="mcss_btnreset" class="btn btn-default" value="Cancel"> </p>';
div+='<ul class="box-footer">';
div+='<li><i class="fa fa-clock-o"></i> save at <span>05:55</span></li>';
div+='<li><i class="fa fa-pencil-square-o"></i> Update : <span>08:20</span></li>';
div+='</ul>';
div+='</div></div>';
$('#Droppable_container').html(div);
var $scope = angular.element('#Droppable_container').scope();
$scope.compileElement('#Droppable_container');
});
?
答案 0 :(得分:1)
亲爱的读者:前两个解决方案失败了
但是,我把他们留在这里,以便解决问题。请向下滚动到&#34; EDIT&#34;看到我的第三个解决方案,它正在运行并且是被接受的解决方案: - )MySQL:#1093 - 您无法指定目标表&#39;更新&#39;用于FROM子句&#34;
的更新
<击> 你说你想用一个随机用户尝试一次,如果有重复的uid + tid,你可以使用uid = 0。为此,您将使用带有连接的UPDATE:
UPDATE quotes upd
CROSS JOIN (SELECT idu FROM users WHERE idu BETWEEN 13 AND 63 ORDER BY rand() LIMIT 1) u
SET upd.uid = u.iud
WHERE upd.uid = 0
AND NOT EXISTS
(
select *
from quotes q
where q.tid = upd.tid
and q.uid = u.uid
);
但是,如果您希望尽可能长时间地找到非重复用户,则可以使用相关子查询:
UPDATE quotes upd
SET uid = coalesce
(
(
SELECT idu
FROM users u
WHERE idu BETWEEN 13 AND 63
AND NOT EXISTS
(
select *
from quotes q
where q.tid = upd.tid
and q.uid = u.uid
)
ORDER BY rand() LIMIT 1
), 0
)
WHERE uid = 0;
区别:您考虑了所有非重复用户。只要至少有一个,您就会更新。对于第一个更新语句,您只需尝试一个随机用户,并且要么是非重复用户,那么您需要更新,否则不会。
击>
INSERT INTO quotes (message, tid, uid)
SELECT q.message, a.id, coalesce(u.idu,0)
FROM quotes_rnd q
JOIN authors a ON a.id > 10
LEFT JOIN users u ON u.idu BETWEEN 13 AND 63
AND NOT EXISTS
(
select *
from quotes q
where q.tid = a.id
and q.uid = u.idu
)
ORDER BY (u.idu IS NULL), rand() LIMIT 1;
(这是一种交叉连接,以便找到所有可能的对,因此在大型表上可能会很慢。)