datashout
datashout

Reputation: 147

How to concatenate a new column on a query result in postgresql

I am running this postgresql 14 query:

select * from tb1 where id in (select id from tb2 where stock = 1313)

this is query is working.

id  speed doors
12   100   23

Now I want to get the same result but with one more column stock, like this:

stock  id  speed doors
 1313  12   100   23

Problem is that in the tb1 there's no column stock

How can I concatenate this column stock to the final result?

important: this query returns only 1 record per stock. But I want to pass several stocks at once, like this:

select * from tb1 where id in (select id from tb2 where stock in (1313,2324,1234))

and get:

stock  id  speed doors
 1313  12   100   23
 2324  15   150   23
 1234  11   100   44

Upvotes: 0

Views: 27

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521314

I would write your query as a join, in which case the column you want would be available:

SELECT t1.*, t2.stock
FROM tb1 t1
INNER JOIN tb2 ON t2.id = t1.id
WHERE t2.stock = 1313;

Upvotes: 1

Related Questions