我想在我的网站上做一个有条件的更新。每当用户单击某个链接时,数据库中的全局计数器变量就会自动递增。
复杂的是,当柜台数量超过我有记录的网站数量,我希望计数器回到1。我有一个更新声明,但它不工作,我不明白为什么。
#Increment global counter, set it back to one when it exceeds the number of websites on record
cursor.execute("UPDATE url_reroute_counter SET counter_num = "+
" CASE"+
" WHEN counter_num>(SELECT count(*) FROM url_reroute_targeturl)"+
" THEN counter_num = 1"+
" ELSE"+
" counter_num = counter_num+1"+
" END"+
" WHERE name_of_counter = 'url_count'")`运行此代码将获得以下异常:
django.db.utils.ProgrammingError: column "counter_num" is of type integer but expression is of type boolean
LINE 1: UPDATE url_reroute_counter SET counter_num = CASE WHEN coun...
^
HINT: You will need to rewrite or cast the expression.我不习惯在任何SQL语言中使用条件词,所以这里的任何hlep都是值得赞赏的。
发布于 2016-06-10 12:25:40
您的查询应该如下
cursor.execute("UPDATE url_reroute_counter SET counter_num = "+
" CASE"+
" WHEN counter_num>(SELECT count(*) FROM url_reroute_targeturl)"+
" THEN 1"+
" ELSE"+
" counter_num+1"+
" END"+
" WHERE name_of_counter = 'url_count'")`和BTW,在一个多行python字符串中,您不需要所有这些+符号。这个:https://stackoverflow.com/a/10660443/267540
https://stackoverflow.com/questions/37747513
复制相似问题