mysql - Unknown Field In Where Clause -


i received database containing table permanent 2 columns quota_our , quota_theirs each row have if 1 not empty other column empty , need have 1 column work tried update 1 of these columns , once records swapped delete other table. thing not mysql need database side project tried nesting selects couldn't set update getting value select within same table tried :

update permanent set quota_our = (     select q     (         select quota_theirs q, ct_num code_x         permanent         quota_our = 0 or quota_our null     ) )     a.code_x = permanent.ct_num ,     permanent.ct_num not null 

but error :

unknown field 'a.code_x' in clause have searched quite related questions , of whom couldn't me in right direction.

i agree, error message bit misleading here. actually, should unknown table a , root cause more clear.

the problem code_x field defined in derived table a created while assign value quota_our field. however, in order reference field (well, table) in where clause, table must listed in table_reference section of update statement:

update [low_priority] [ignore] table_reference     set assignment_list     [where where_condition]     [order ...]     [limit row_count] 

you can around issue using multi-table update statement avoiding need subquery:

update permanent p1  inner join permanent p2 on p2.ct_num = p1.ct_num set p1.`quota_our` = p2.`quota_theirs` p1.ct_num not null , (p2.quota_our= 0 or p2.`quota_our` null) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -