简体   繁体   English

如何在PHP中加入2个mysql查询作为hetml返回值

[英]how to join 2 mysql queries in php as hetml return value

Indivudually these queries run 100% 这些查询分别运行100%

query1 查询1

SELECT year_entered, 
   month_entered,
   IFNULL(amount - @n, 0) amount_diff,
   @n := amount current_amount
  FROM
 (SELECT YEAR(date_entered) year_entered, 
     MONTHNAME(date_entered) month_entered, 
     SUM(amount) amount
FROM opportunities
INNER JOIN opportunities_cstm
ON ( opportunities.id = opportunities_cstm.id_c)
WHERE 
YEAR(opportunities.date_entered) >= YEAR(CURDATE())
AND  (`opportunities`.`deleted` <> '1') AND 
(opportunities_cstm.interest_level_c = 'Ultraspin'
OR 
opportunities_cstm.interest_level_c = 'Mycelx')
GROUP BY YEAR(date_entered), MONTH(date_entered)) q, 
(SELECT @n := NULL) n

query 2 查询2

SELECT year_entered, 
   month_entered,
   IFNULL(id - @n, 0) id_diff,
       @n := id current_id
  FROM
 (SELECT YEAR(date_entered) year_entered, 
         MONTHNAME(date_entered) month_entered, 
         COUNT(id) id
    FROM opportunities
    INNER JOIN opportunities_cstm
    ON ( opportunities.id = opportunities_cstm.id_c)
 WHERE 
 YEAR(opportunities.date_entered) >= YEAR(CURDATE())
 AND  (`opportunities`.`deleted` <> '1') AND 
 (opportunities_cstm.interest_level_c = 'Ultraspin'
 OR 
 opportunities_cstm.interest_level_c = 'Mycelx')
   GROUP BY YEAR(date_entered), MONTH(date_entered)) q, 
 (SELECT @n := NULL) n
 ON year_entered = year_entered

I need to how the amount differences and the count differences in one query, Want to user jasper reports to report this out, or if someone could help in php return rows? 我需要一个查询中的数量差异和计数差异如何,是否希望用户使用jasper报告将其报告出来,或者是否有人可以帮助php返回行?

just dont know how to get both queries in and returning them next to each other 只是不知道如何获取两个查询并彼此相邻返回

Try this: 尝试这个:

    SELECT year_entered, 
   month_entered,
   query1.amount_diff,
   query2.id_diff FROM
        (SELECT year_entered, 
           month_entered,
           IFNULL(amount - @n, 0) amount_diff,
           @n := amount current_amount
          FROM
         (SELECT YEAR(date_entered) year_entered, 
             MONTHNAME(date_entered) month_entered, 
             SUM(amount) amount
        FROM opportunities
        INNER JOIN opportunities_cstm
        ON ( opportunities.id = opportunities_cstm.id_c)
        WHERE 
        YEAR(opportunities.date_entered) >= YEAR(CURDATE())
        AND  (`opportunities`.`deleted` <> '1') AND 
        (opportunities_cstm.interest_level_c = 'Ultraspin'
        OR 
        opportunities_cstm.interest_level_c = 'Mycelx')
        GROUP BY YEAR(date_entered), MONTH(date_entered)) q, 
        (SELECT @n := NULL) n) query1 JOIN (
            SELECT year_entered, 
               month_entered,
               IFNULL(id - @n, 0) id_diff,
                   @n := id current_id
              FROM
             (SELECT YEAR(date_entered) year_entered, 
                     MONTHNAME(date_entered) month_entered, 
                     COUNT(id) id
                FROM opportunities
                INNER JOIN opportunities_cstm
                ON ( opportunities.id = opportunities_cstm.id_c)
             WHERE 
             YEAR(opportunities.date_entered) >= YEAR(CURDATE())
             AND  (`opportunities`.`deleted` <> '1') AND 
             (opportunities_cstm.interest_level_c = 'Ultraspin'
             OR 
             opportunities_cstm.interest_level_c = 'Mycelx')
               GROUP BY YEAR(date_entered), MONTH(date_entered)) q, 
             (SELECT @n := NULL) n
             ON year_entered = year_entered
        ) query2 ON query1.year_entered = query2.year_entered AND query1.month_entered = query2.month_entered
SELECT year_entered, 
       month_entered,
       IFNULL(amount - @n, 0) amount_diff,
       IFNULL(id - @a, 0) id_diff,
       @n := amount current_amount,
       @i := id current_id
FROM (SELECT YEAR(date_entered) year_entered, 
             MONTHNAME(date_entered) month_entered, 
             SUM(amount) amount,
             COUNT(id) id
      FROM opportunities
      INNER JOIN opportunities_cstm
      ON ( opportunities.id = opportunities_cstm.id_c)
      WHERE YEAR(opportunities.date_entered) >= YEAR(CURDATE())
            AND  (`opportunities`.`deleted` <> '1')
            AND (opportunities_cstm.interest_level_c = 'Ultraspin'
            OR opportunities_cstm.interest_level_c = 'Mycelx')
      GROUP BY YEAR(date_entered), MONTH(date_entered)) q,
     (SELECT @n := NULL, @i := NULL) n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM