I'm trying to make a query that returns the result of two different querys.
I have this one :
'test'
to return :
SELECT idtravel, travel, status FROM travel_db.travel
inner join travel_db.status
on travel.status_idstatus=status.idstatus
order by idtravel desc
Also have this query :
idtravel | travel | status
1 London Completed
2 NY Planned
3 Lisbon Completed
which returns :
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
My point is to merge this two querys to have something like this :
Value
600
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 :
idtravel | travel | status | Value
1 London Completed 600
2 NY Planned 1500
3 Lisbon Completed 150
to return :
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
It's only missing the field status
*Edit 14-05-15: Guys, finaly it's completed.
Here's the final query :
idtravel | travel | value
1 London 600
2 NY 1500
3 Lisbon 150
returning :
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
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.
答案 0 :(得分:2)
How about this?
getPosts
or you replace INNER JOIN with LEFT JOIN if your secondary columns may not have a value for all travel ids.
答案 1 :(得分:0)
Assuming that parseFloat
field belongs to idtravel
and serves as a foreign key to all travel_db.travel
tables.
desp