Margus Margus - 4 years ago 129
PHP Question

join / union two query

first query:

SELECT artikkel_id, liige_id, summa, date, SUM(summa) as papp
FROM rent WHERE date < NOW() group by liige_id


result :
enter image description here
second query:

SELECT artikkel_id, maksja_id, summa, timestamp, SUM(summa) as peaks
FROM tulu group by maksja_id


result:enter image description here

in tables maksja_id & liige_id is same.

Point of "join" is, that I can to compare and display liige_id/maksja_id "papp" and "peaks" values and mark in table row with color, when papp < peaks then with red, when = the green etc.

Answer Source

You wanted to join both the result set saying

select xx.artikkel_id, l.nimi, xx.summa, xx.`date`, xx.papp,
yy.summa as yysumma, yy.`timestamp`, yy.peaks
from (SELECT artikkel_id, liige_id, summa, date, SUM(summa) as papp 
        FROM rent WHERE date < NOW() group by liige_id) xx
join (SELECT artikkel_id, maksja_id, summa, timestamp, 
 SUM(summa) as peaks FROM tulu group by maksja_id) yy 
 on xx.liige_id = yy.maksja_id
join liige l on l.liige_id = xx.liige_id;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download