开发者

insert/select with a condition

I'm not sure if this is possible but is there a way to select A unless its null then select B instead?

I am writing a trigger, my code is

insert into tbl_a(userid, obj)
select p.author, new.id 
FROM开发者_C百科 user_comment AS p 
WHERE p.id=new.parent

however new.parent is a nullable long (i'll switch to foreign key once supported in system.data.sqlite) if its null i get 0 results and no insert. I would like to use join media as m on m.id=new.media_id and return m.user_id if new.parent is null. So how do i write the select to return m.author if parent isnt null (which i'll return p.author like the above)


You can use IFNULL(col1, col2). If col1 is not null, it is returned. If col1 is null, col2 is returned.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜