MySQL Insert where not exists query won't insert if even one row exists -
i having pretty complicated query. have table car parts (parts_list, 600 rows) contains information part it's name , if it's motor dependent or not. motor dependency having 2 different queries, 1 no motor dependency (0, save boolean). of parts can disassembled , broken more parts, that's why saving parts tree , in query take parts can't disassembled (tree leaves). table represents list of possible parts. each car model save row in table (parts) , stick parts_list_id , model_id , price , quantity. if run query generate 500 rows(taking leaf parts) in table "parts" , need. 500 (leaf) parts model id. generate row model specific part. , query doesn't make rest 499 rows. works if not exists select query gives 0. if 1 row exists doesn't insert rest. doesn't make sense me, because shouldn't check different values loop?
insert parts (parts_list_id, model_id, motor_id) select orig1.id, '" . $this->model_id . "', '0' parts_list orig1 left join parts_list orig2 on ( orig1.id = orig2.parent_id ) orig2.id null , orig1.motor_dependent = '0' , not exists ( select t1.id parts_list t1 left join parts_list t2 on ( t1.id = t2.parent_id ) left join parts on ( parts.parts_list_id = t1.id ) t2.id null , t1.motor_dependent = '0' , parts.parts_list_id = t1.id , parts.model_id = :model_id )
well, sql statement seems fine. if there 1 row, not exists returns false. , correct. no 1 row inserted. perhaps want put different checks using parts_list_id not in (your subquery) instead of not exists.
not exists express condition set whole, not in used determine if set has right items.
i hope right. little bit difficult understand domain single statement.
Comments
Post a Comment