Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two queries - how?

Tags:

sql

postgresql

I have the following queries:

Query 1:

Select * from T10,T11,T12,T13,T14 
where T10.C0 = T11.C0 and T11.C1 = T12.C0 
and T12.C1 = T13.C0 and T13.C1 = T14.C0; 

Query 2:

Select * from T20,T21,T22,T23,T24 
where T20.C0 = T21.C0 and T21.C1 = T22.C0 
and T22.C1 = T23.C0 and T23.C1 = T24.C0;

How can I combine these 2 queries to show all the values of these tables? I want the join to be on T10.C1 = T20.C1.

When trying union I get a warning about not having the same number of columns, which is true, these tables are not the same


union

Select * from 
"ProductConfig","Board","PcbBuild","Model","TcssCalib" 
where "Model"."idModel" = "PcbBuild"."Model" and "Board"."PcbBuild" = "PcbBuild"."idPcbBuild" 
and "Board"."idBoard" = "TcssCalib"."Board" and "ProductConfig"."TcssCalib" = "TcssCalib"."idTcssCalib"

union

Select * from"ProductBuild","TxResultsLink","TxResults","DspValues" where "ProductBuild"."idProductBuild"
 = "TxResultsLink"."ProductBuild" and "TxResults"."idTxResults" = "TxResultsLink"."TxResults" 
and "TxResults"."DspValues" = "DspValues"."idDspValues";

here i want ProductBuild.Productconfig joined with ProductConfig.idProductConfig

gives an error:

[Err] ERROR: each UNION query must have the same number of columns


When I try inner join I get a syntax error at or near inner

Is there a way to join these 2 queries together?

like image 996
Moonlight Avatar asked Aug 17 '26 15:08

Moonlight


1 Answers

This is what you want:

Select * from T10,T11,T12,T13,T14,T20,T21,T22,T23,T24
where T10.C0 = T11.C0 and T11.C1 = T12.C0 
and T12.C1 = T13.C0 and T13.C1 = T14.C0
and T10.C1 = T20.C1 and T20.C0 = T21.C0 and T21.C1 = T22.C0 
and T22.C1 = T23.C0 and T23.C1 = T24.C0;

and another, more readable way to put it, is like this:

select * from T10
  inner join T11 on T10.C0 = T11.C0
  inner join T12 on T11.C1 = T12.C0 
  inner join T13 on T12.C1 = T13.C0 
  inner join T14 on T13.C1 = T14.C0
  inner join T20 on T10.C1 = T20.C1
  inner join T21 on T20.C0 = T21.C0
  inner join T22 on T21.C1 = T22.C0 
  inner join T23 on T22.C1 = T23.C0
  inner join T24 on T23.C1 = T24.C0;

For UNION to result, the number of columns in the tables needs to be the same and from the same/convertable datatype.

Select * from T10,T11,T12,T13,T14 
where T10.C0 = T11.C0 and T11.C1 = T12.C0 
and T12.C1 = T13.C0 and T13.C1 = T14.C0

UNION

Select * from T20,T21,T22,T23,T24 
where T20.C0 = T21.C0 and T21.C1 = T22.C0 
and T22.C1 = T23.C0 and T23.C1 = T24.C0;

Select * from 
"ProductConfig","Board","PcbBuild","Model","TcssCalib","ProductBuild","TxResultsLink","TxResults","DspValues"
where "Model"."idModel" = "PcbBuild"."Model" and "Board"."PcbBuild" = "PcbBuild"."idPcbBuild" 
and "Board"."idBoard" = "TcssCalib"."Board" and "ProductConfig"."TcssCalib" = "TcssCalib"."idTcssCalib" and "ProductBuild"."idProductBuild"
 = "TxResultsLink"."ProductBuild" and "TxResults"."idTxResults" = "TxResultsLink"."TxResults" 
and "TxResults"."DspValues" = "DspValues"."idDspValues"
and "ProductBuild"."ProductConfig" = "ProductConfig"."idProductConfig";
like image 94
aF. Avatar answered Aug 20 '26 04:08

aF.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!