Postgresql: select return ARRAY

I have a table like:

CREATE TABLE tbl_temp (id serial, friend_id int, name varchar(32)); 

I would like to run the following SQL:

 PREPARE x AS SELECT {$1,friend_id} FROM tbl_temp WHERE id = ANY($2); EXECUTE x(33, ARRAY[1,2,3,4]) 

Basically, I'm looking for an instruction that will return an array of two ints to me, the first of which will be the user, and the second will be from a table column, such as friend_id.

Is this possible in PostgreSQL?

Please inform.

Results SELECT ($ 1, friend_id) FROM tbl_temp;

 EXECUTE x(44); row -------- (44,1) (44,2) (44,3) (3 rows) 

If I use PQgetvalue(PGres, 0, 0) What the result will look like: {44,45} or like(44,45)

Yours faithfully,
Mayank

+8
arrays sql postgresql
source share
2 answers

I think you want to use the array constructor syntax:

 SELECT ARRAY[$1, friend_id] FROM tbl_temp WHERE id = ANY($2) 
+16
source share

I'm not sure I understand what you want ...

to return an array do this.

 SELECT (44, "friend_id") FROM "tbl_temp" WHERE id = ANY(ARRAY[1,2,3,4]); 
+4
source share

All Articles