join / union两个查询

时间:2016-01-03 23:15:06

标签: php html mysql

第一次查询:

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

结果: enter image description here 第二个查询:

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

结果:enter image description here

表格中的

maksja_id&amp; liige_id是一样的。

点&#34;加入&#34;是的,我可以比较和显示liige_id / maksja_id&#34; papp&#34;和#34;峰值&#34;当papp&lt;时,值和用颜色标记在表行中峰值然后是红色,当=绿色等时

3 个答案:

答案 0 :(得分:1)

您希望join结果集说明

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;

答案 1 :(得分:0)

加入和联盟会产生完全不同的结果。

根据您希望结果如何显示,您可以做什么

  1. Union会给出两者的结果,根据你的例子给你6条记录

    SELECT artikkel_id, liige_id as maksja_id, summa, date, SUM(summa) as papp 
    FROM rent WHERE date < NOW() group by liige_id
    union 
    SELECT artikkel_id, maksja_id, summa, timestamp, SUM(summa) as papp 
    FROM tulu group by maksja_id
    
  2. 加入将为您提供3条记录而不是上述6条记录的结果。

  3. 为此你可以使用@Rahul的回答

    就像Rahul一样,我认为你想要加入 联盟

答案 2 :(得分:0)

MySQL有一个SIGN function,它会将其参数(例如xx.papp-yy.peak)分类为正,零或负。