Home > Enterprise >  Passing result of one table to another table in Database?
Passing result of one table to another table in Database?

Time:11-04

I have the following scenario: I am trying to pass result of one query to another , In this case I am trying to pass view_id in another query since this are ids they are unique and cant be duplicate in any case .

select view_id from view where view_query_id = "18711987173" 

select queue_id from queue where queue_view = view_id

`

I saw some examples and I tried executing them something like as

select view_id from view where view_query_id = "18711987173 exists (select queue_id from queue where queue_view = view.view_id)

But surely this didnt helped :)

CodePudding user response:

You can use a common table expression

WITH temp AS (SELECT view_id FROM view WHERE view_query_id = "18711987173")
SELECT queue_id FROM queue q INNER JOIN temp t ON q.queue_id = t.view_id;

This should work regardless of what relationship is between those tables. You can replace the JOIN by WHERE..IN, but this way seems cleaner and takes care if the no. of values in IN becomes too large.

CodePudding user response:

Use table expression

WITH temp AS (SELECT view_id FROM view WHERE view_query_id = "18711987173")
SELECT queue_id FROM queue q INNER JOIN temp t ON q.queue_id = t.view_id;
  • Related