-1

These are my tables:

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  

I tried this:

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

I woud like to update those question that has got more answers than it's counted in:
tbl_questions => answercount

How can I update the rows which have got more questions than it's counted? (Without php loop)

9
  • stackoverflow.com/questions/1262786/… Commented May 22, 2013 at 10:41
  • It would help me more if you could tell me why is not soultion dont work. Commented May 22, 2013 at 11:09
  • When you say it's not working do you mean that the query won't run, or that the incorrect rows are updated? Commented May 22, 2013 at 11:17
  • The query don't run, don't update anything. Commented May 22, 2013 at 11:28
  • You're using a field called id that doesn't exist in the table tbl_questions, also what is num in your query? Commented May 22, 2013 at 11:42

1 Answer 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'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.