How to update SQL with single query and not with php loop?
这些是我的表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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 |
我试过这个:
1 2 3 4 5 6 7 8 9
| 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 => answersercount
如何更新比计算的问题更多的行? (没有php循环)
-
stackoverflow.com/questions/1262786/…
-
如果你能告诉我为什么没有灵魂不行,那对我会更有帮助。
-
如果你说它不起作用你的意思是查询不会运行,或者更新错误的行?
-
查询不运行,不更新任何内容。
-
您正在使用表tbl_questions中不存在的名为id的字段,以及查询中的num是什么?
-
num将是answercount和... id。 这有效吗? tbl_questions.qid?
-
不,它说:Column 'qid' in on clause is ambiguous
-
当您需要时可以在数据库中查询计数时,为什么要尝试维护并行计数?
-
因为这个数据库已经有超过100万的rekords,它会越来越大......小优化......
1 2 3 4 5 6 7 8
| 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' |