문제

Suppose I have a table customer with a field number_of_times_contacted. Now I want to update this field value by adding one to the existing value of every record with last name equal to Smith. In pseudocode I want to do something like:

db(db.customer.last_name == "Smith").update(
     number_of_times_contacted = number_of_times_contacted + 1)

What is the expression to do this? Or do I have do get the customer record first and then update?

Also would the expression be any different for updating a single record by id?

도움이 되었습니까?

해결책

db(db.customer.last_name == "Smith").update(
   number_of_times_contacted=db.customer.number_of_times_contacted + 1)

No different if updating a single record by ID.

Regarding your follow-up question, the exact function(s) will depend on the particular database system, but something like this:

from gluon.dal import Expression
db(db.customer.last_name == "Smith").update(
   last_name=Expression(db, 'LTRIM(RTRIM(last_name))'))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top