在 Postgres 的 WHERE ANY 中使用 SELECT INTO 中的列
我在过程中有一个如下所示的 SELECT INTO
查询:
declare
employee_data record;
item record
begin
select * into employee_data from employees where emp_id=10;
现在我在另一个查询中使用 employee_data
变量中的列:
FOR item in
SELECT CONCAT(E.first_name,' ',E.last_name) AS employee_name,
E.email AS employee_email
INTO notify_users_data
FROM employee_info E
WHERE E.emp_id = ANY(ARRAY employee_data.notify_users)
LOOP
这里是 notify_usersemployee
表中的 code> 列的类型为 jsonb
,它是一个类似于 ["2001", "3457"]
的数组
当我尝试保存这个查询时,它显示语法错误
error:employee_data 处或附近的语法错误: WHERE E.emp_id = ANY(ARRAY employee_data
如何在 WHERE ANY 中使用结果?
I have a SELECT INTO
query like the following in a procedure :
declare
employee_data record;
item record
begin
select * into employee_data from employees where emp_id=10;
Now I am using a column from the employee_data
variable in another query:
FOR item in
SELECT CONCAT(E.first_name,' ',E.last_name) AS employee_name,
E.email AS employee_email
INTO notify_users_data
FROM employee_info E
WHERE E.emp_id = ANY(ARRAY employee_data.notify_users)
LOOP
Here, the notify_users
column in employee
table is of type jsonb
and it is an array like ["2001", "3457"]
When I try to save this query, it is showing a syntax error
error:syntax error at or near employee_data: WHERE E.emp_id = ANY(ARRAY employee_data
How can I use the result in WHERE ANY ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
附加关键字
ARRAY
没有任何意义。① 如果
notify_users
是 Postgres 数组类型,则按原样使用。请参阅:② If
notify_users
是jsonb
形式的["2000", "3000"]
,而emp_id
是字符串类型(不是数字类型!),可以使用?
运算符,如图所示。FOR
循环 使用隐式游标。每行都分配给item
- 如顶部声明的那样。额外的INTO notification_users_data
没有任何意义。其他一些小的编辑。
可以进一步简化:
This should work:
The additional keyword
ARRAY
made no sense.① If
notify_users
is a Postgres array type, use it as is. See:② If
notify_users
is typejsonb
of the form["2000", "3000"]
, andemp_id
is a string type (not a numeric type!), you can use the?
operator as demonstrated.A
FOR
loop works with an implicit cursor. Each row is assigned toitem
- as declared at the top. The additionalINTO notify_users_data
made no sense.Some other minor edits.
Can be simplified further:
如果您的变量是数组,则可以使用
UNNEST
函数将数组元素转换为选定的表数据。示例:
如果您的变量采用这种格式
["2001", "3457"]
那么您可以首先使用jsonb_array_elements_text
函数,然后使用UNNEST
函数。例子:
If your variable is an array then you can use the
UNNEST
function to convert array elements to the selected table data.Example:
If your variable in this format
["2001", "3457"]
then you can firstly usejsonb_array_elements_text
function and after thenUNNEST
function.Example: