带条件插入/选择
我不确定这是否可行,但有没有办法选择 A,除非它为空,然后选择 B?
我正在编写一个触发器,
insert into tbl_a(userid, obj)
select p.author, new.id
FROM user_comment AS p
WHERE p.id=new.parent
但是我的代码是 new.parent
是一个可以为 null 的 long (一旦 system.data.sqlite 支持,我将切换到外键) 如果它为空,我得到 0 个结果并且没有插入。我想在 m.id=new.media_id 上使用 join media as m
并在 new.parent
为 null 时返回 m.user_id
。那么,如果父项不为空,我该如何编写选择以返回 m.author
(我将像上面一样返回 p.author
)
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 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
IFNULL(col1, col2)
。如果col1
不为空,则返回。如果col1
为 null,则返回col2
。You can use
IFNULL(col1, col2)
. Ifcol1
is not null, it is returned. Ifcol1
is null,col2
is returned.