开发者

IF EXISTS do this.. stumped =x

So I want to insert into a table if there isn't an existing row matching conditions such as only allowing one "like" per 24-hours for the same product.

I have it nailed down to this so far, but now I need to take the hash and join in the id when making the insert. I've read that using DUAL doesn't allow joining in from other tables so I'm stumped here.

Any tips will be greatly appreciated :) Thanks!

INSERT INTO platform.contentLikes                                              

SELECT contents.contentId, 500000, NOW()         开发者_如何学Python                              

FROM DUAL                                                                      

WHERE NOT EXISTS (  SELECT contentId, contents.contentId FROM platform.contentLikes                                                                            

    INNER JOIN platform.contents ON contents.hash = 'de9f21c14c1ab0bcca014825f8b09e76'                                                                        

    WHERE contentId = contents.contentId AND userId = 50000 )


INSERT INTO platform.contentLikes                                              

SELECT rs.contentId, 500000, NOW()                                       

FROM (  SELECT contents.contentId
    FROM platform.contents LEFT JOIN platform.contentLikes ON contentLikes.contentId = contents.contentId  AND contents.hash = 'de9f21c14c1ab0bcca014825f8b09e76' AND userId = 50000
    WHERE contentLikes.contentId IS NULL)
    rs

another option would be to get the contentID in your php code, and create your insert query with the contentID coded in:

$sql = 'INSERT INTO platform.contentLikes
SELECT ' . $contentId . ', 500000, NOW()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜