mysql - insert into table only if not exist on other table -


i'm trying following thing: have 2 tables: reportimage (imageid, reportid, counter) , userreportedimages (imageid, userid)

i want every user able report image once - means first want check if there row in 'userreportedimages' values (imageid, userid) if nothing, else create row in 'reportimage' values (imageid, reportid, counter), if such row exist (other user reported image) want raise counter.

so far before checking same user report had following statement:

insert reportimage values (imageid,reportid,1) on duplicate key update counter = counter+1 

this statement working fine.

i tried change statement first check if row exist on other table, didn't manage it, can me?

first, need define unique constraint or compund column primary key on table reportimage,

alter table reportimage add contraint tb_uq unique(imageid, reportid) 

give try,

insert reportimage(imageid, reportid, counter) select  'imageid here' imageid,         'userid here' reportid,         1 counter    userreportedimages         left join reportimage b             on  a.imageid = b.imageid ,                 a.userid = b.reportid ,                 a.imageid = 'imageid here' ,                 a.userid = 'userid here'   b.imageid null or          b.reportid null on duplicate key update counter = values(counter) + 1 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -