I'm trying to make a query that returns the result of two different querys.
I have this one :
SELECT idtravel, travel, status FROM travel_db.travel
inner join travel_db.status
on travel.status_idstatus=status.idstatus
order by idtravel desc
to return :
idtravel | travel | status
1 London Completed
2 NY Planned
3 Lisbon Completed
Also have this query :
select sum(value) as total
from
( select a.value
from desp_housing a
where travel_idtravel = 1
union all
select t.value
from desp_transport t
where travel_idtravel = 1
union all
select tu.value
from desp_turism tu
where travel_idtravel = 1
) z
which returns :
Value
600
My point is to merge this two querys to have something like this :
idtravel | travel | status | Value
1 London Completed 600
2 NY Planned 1500
3 Lisbon Completed 150
Can anyone help?
*Edit 13-05-15: After some ideas, i'm one field short from my final result. Here's where i'm at :
select idtravel, travel, sum(value) as total
from travel_db.travel
inner join
( select a.travel_idtravel, a.value
from desp_housing a
union all
select t.travel_idtravel, t.value
from desp_transport t
union all
select tu.travel_idtravel, tu.value
from desp_turism tu
union all
SELECT idtravel, travel
FROM travel_db.travel
) z
on travel.idtravel=z.travel_idtravel
group by travel_idtravel
to return :
idtravel | travel | value
1 London 600
2 NY 1500
3 Lisbon 150
It's only missing the field status
*Edit 14-05-15: Guys, finaly it's completed.
Here's the final query :
Select idtravel, travel, sum(value) as total, status
From travel_db.travel
inner join
(select a.travel_idtravel, a.value, a.status_idstatus
from desp_housingo a
union all
select t.travel_idtravel, t.value, t.status_idstatus
from desp_transport t
union all
select tu.travel_idtravel, tu.value, t.status_idstatus
from desp_turism tu
union all
select idtravel, travel, status_idstatus
from travel_db.travel
) z
on travel.idtravel=z.travel_idtravel
inner join travel_db.status
on travel.status_idstatus=status.idstatus
group by travel_idtravel
returning :
idtravel | travel | value | status
1 London 600 | Completed
2 NY 1500| Planned
3 Lisbon 150 | Completed
The thing is, because of how the query is made you have to select fields that you are not going to use (a.status_idstatus, t.status_idstatus, t.status_idstatus) to ensure that you have the same number of fields. Maybe can be optimized but for now is working.