Is there a way to do a multi table query and get result just from specific tables?

StackOverflow https://stackoverflow.com/questions/23586991

  •  19-07-2023
  •  | 
  •  

Frage

I am trying to do a multi query but I don't want to use sub queries i.e:

SELECT column1 
FROM table1 
WHERE 
    EXISTS (SELECT column1 FROM table2 WHERE table1.column1 = table2.column1);)

I thought of using a JOIN but so far my best result was this:

SELECT * 
FROM table1 
JOIN table2 ON table1.t1id = table2.t2id 
WHERE table1.id = 5;

This would be good except of the fact that I get a duplicate column (the id in table 1 and 2 are foreign keys).

How do I remove the duplicate column if possible?

UPDATE:

Table1:
 tableA_ID, TABLEB_ID
         1,    1
         1,    4
         3,    2
         4,    3

TableA: ID, COL1, COL2
         1, A, B
         2, A, B
         3, A, B
         4, A, B   

TableB: ID, Col3, COL4 
         1, C, D
         2, C, D
         3, C, D
         4, C, D   

I want to get all or some of the columns from TableA according to a condition

Sample: Lets say the condition is that tableA_ID = 1 which will result in the 2 first rows in the table then I want to get all or some of the columns in TableA that respond to the ID that I got from Table1.

Sample: The result from before was [{1,1}{1,4}] which means I want from TableA the results:

TableA.ID, TableA.COL1, TableA.COL2  
    1,A,B  
    4,A,B  

The actual results I get is:

Table1.tableA_ID, Table1.TABLEB_ID, TableA.ID, TableA.COL1, TableA.COL2  
    1,1,1,A,B  
    1,4,4,A,B
War es hilfreich?

Lösung

Is this what you're looking for?

select a.id, a.column1, b.column2 
from table1 a 
left join table2 b on a.id = b.otherid;

Andere Tipps

You can't change the column list of a query based on the values it returns. It just isn't the way that SQL is designed to operate. At best, you can return all of the columns from the second table and ignore the ones that aren't relevant based on other values in that row.

I'm not even sure how a variable column list would work. In your scenario, you're looking for two discrete values separately. But that's not the only scenario: what if the condition is tableA_ID in (1,2). Would you want different numbers of columns in different rows as part of a single result set?

Getting just the columns you want (just from specific tables, as you say) is the easy part (btw -- don't use '*' if you can help it -- topic for another discussion):

SELECT
   A.ID,
   A.COL1,
   A.COL2
FROM
   TABLE1 Bridge

   LEFT JOIN TABLEA A
   ON Bridge.TABLEA_ID = A.ID

   LEFT JOIN TABLEB B
   ON Bridge.TABLEB_ID = B.ID

Getting the rows you want will be the harder part (influenced by your choice of joins, among several other things).

I think you'll need to select only the fields of table A and use a distinct clause. Rest of your query will remain as it is. i.e.

SELECT distinct table1.* 
FROM table1 
JOIN table2 ON table1.t1id = table2.t2id 
WHERE table1.id = 5;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top