带条件插入/选择

发布于 2024-08-19 22:10:19 字数 503 浏览 4 评论 0原文

我不确定这是否可行,但有没有办法选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2024-08-26 22:10:19

您可以使用 IFNULL(col1, col2)。如果col1不为空,则返回。如果 col1 为 null,则返回 col2

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文