-1

这些是我的表:

tbl_answers => 
  aid     
  qid     
  answer     
  uid     
  dateposted     
  emailnotify
  namedisplay
  status
  isbestanswer 


tbl_questions =>
  qid
  question
  detail
  mcid
  cid
  uid     
  answercount
  dateposted
  status
  showname
  emailnotify
  question_type_id  

我试过这个:

UPDATE tbl_questions
JOIN (
SELECT id, COUNT(*) AS n 
FROM tbl_questions JOIN tbl_answers ON qid = tbl_questions.qid 
WHERE answercount = "0" 
GROUP BY id
) AS T USING (id)
SET num = n 
WHERE n > 0

我想更新那些得到的答案多于计算在内的问题:
tbl_questions => answercount

如何更新问题多于计数的行?(没有 php 循环)

4

1 回答 1

0
UPDATE tbl_questions
  JOIN (
    SELECT tbl_questions.qid, COUNT(*) AS n 
    FROM tbl_questions JOIN tbl_answers ON tbl_answers.qid = tbl_questions.qid  
    GROUP BY qid
  ) AS T USING (qid)
SET answercount = n 
WHERE n > 0'
于 2013-05-22T12:29:34.807 回答