Postgresql中的IF语句(IF statement in Postgresql)

编程入门 行业动态 更新时间:2024-10-18 18:23:32
Postgresql中的IF语句(IF statement in Postgresql)

我为我们的系统提供了一个饲料表

它看起来像这样:

id | owner_id | user_id | feed_type_id | object_id | created_at | updated_at

feed_type_id - 表示存储在不同表中的对象类型(文章或job_post)

object_id是对象ID本身,但如果此对象仍处于活动状态,我不知道。 所以我已经获得了不同类型对象的所有活动ID,但是当我试图用IF语句构建查询时,就像这样

SELECT * FROM feeds WHERE "owner_id" in (2, 4, 1356) AND object_id IN ( IF `feed_type_id` = 1 THEN (2,3,23,33) ELSE (4,5,77,33) end if)

它不工作。

有人能给我一个想法如何做到这一点?

I have a table with feed for our system

it's look like this:

id | owner_id | user_id | feed_type_id | object_id | created_at | updated_at

feed_type_id - represent type of object that is stored in different table (article or job_post)

object_id is the object ID itself, but I don't have a clue if this object is still active. So I have get all active id for different type of object, but when I am trying to build query with IF statement something like this

SELECT * FROM feeds WHERE "owner_id" in (2, 4, 1356) AND object_id IN ( IF `feed_type_id` = 1 THEN (2,3,23,33) ELSE (4,5,77,33) end if)

it's not working.

Can someone give me an idea how to do this?

最满意答案

IF语句不是SQL语言的一部分,而是过程语言的一部分。 因此,您不能直接在查询中使用它们。

您正在查找的查询是这样的:

SELECT * FROM feeds WHERE "owner_id" in (2, 4, 1356) AND ( ("feed_type_id" = 1 AND "object_id" IN (2, 3, 23, 33)) OR ("feed_type_id" != 1 AND "object_id" IN (4, 5, 77, 33)) )

IF statements are not part of the SQL language, but part of the procedural language. As such, you can't use them directly in queries.

The query you're looking for is something like this:

SELECT * FROM feeds WHERE "owner_id" in (2, 4, 1356) AND ( ("feed_type_id" = 1 AND "object_id" IN (2, 3, 23, 33)) OR ("feed_type_id" != 1 AND "object_id" IN (4, 5, 77, 33)) )

更多推荐

本文发布于:2023-08-05 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   Postgresql   statement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!