PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

Outer Join 3 oder mehr Tabellen in Laravel 8

Sie können es einfacher lösen:

select ts recordtime, max(to1) tempout, max(to2) tempoutstamb, max(to3) tempoutstamb2
from (
  select ts, tempout to1, cast (null as numeric(10,1)) to2, cast (null as numeric(10,1)) to3
  from table1
union all 
  select ts, null, tempout, null
  from table2
union all
  select ts, null, null, tempout
  from table3
) tt
group by ts
order by ts; 

Sie finden es in fiddle https://www.db-fiddle.com/f /eJsPZijRnQFGXugLGHnn93/0

HINWEIS:Ich bin davon ausgegangen, dass Nullwerte, die als '-' angezeigt werden, nur eine Ausgabeformatierung sind. Wenn dies nicht der Fall ist, kann die Ausgabe mit NULL in '-' konvertiert werden.

HINWEIS2:Ich weiß nicht, wie ich in Laravel/PHP-Code konvertieren soll, hoffentlich haben Sie eine bessere Idee.