Home > Back-end >  How to get the full output of a query even when the constraint parameter shows the same result
How to get the full output of a query even when the constraint parameter shows the same result

Time:02-03

i have the following query:

select  names as "names"  from tbl1 where id_name  in
                    (select owner_id from tbl2 where id in
                    (select  product_id FROM tbl3 where tbl3.id IN (10, 11, 15)))

and I get as response because id 10 and 11 results in same name Ford

names
Ford
Fiat

What I want to do:

  1. to get the two lines as response, even if they are the same:

      names
      Ford
      Ford
      Fiat
    
  2. I want to get the tbl3.id in the result, like this:

    id    names
    10    Ford
    11    Ford
    15    Fiat
    

CodePudding user response:

As stated by @IMSoP you need to JOIN your tables. Try something like this :

SELECT t3.id, t1.names
  FROM tbl1 AS t1
 INNER JOIN tbl2 AS t2
    ON t1.id_name = t2.owner_id
 INNER JOINO tbl3 AS t3
    ON t2.id = t3.product_id
 WHERE t3.id IN (10, 11, 15)
  • Related