开发者

insert/select with a condition

开发者 https://www.devze.com 2022-12-18 09:41 出处:网络
I\'m not sure if this is possible but is there a way to select A unless its null then select B instead?

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

精彩评论

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