php - mysql select/delete using join over four tables -
i have 4 tables (in [] columns):
users [id]
products [id]
productratings [id,value,user,product]
comments [id,product,user]
i select/and delete productratings there no associated comment same user product. is, if user has rated product did not comment, rating should deleted.
i believe achieve using 2 queries, first:
select user, product productratings
and each row:
select count(*) comments product=productratings.product , user=productratings.user
and like
if $queryabove==0 : delete productratings id=productratings.id
i solve via join , learn more example rather dig through join tutorials.
you need productratings , comments table - following works:
delete pr productratings pr left join comments c on pr.userid = c.userid , pr.productid = c.productid c.productid null
and there's demo here: http://sqlfiddle.com/#!9/89575/1
Comments
Post a Comment