具有多个SELECT

时间:2016-09-12 07:25:50

标签: mysql sql

我在我的应用程序中有表格,我想要的是提供表格,其中一行具有以下计算的值:

SELECT SUM (val) FROM report_orbiting_vals rov 
  WHERE rov.orbiting_group_type_id = ? AND rov.orbiting_group_indice_id = ?

对于第一行,我想呈现4个值,每个值看起来都是这样:

SELECT SUM (val) FROM report_orbiting_vals rov 
  WHERE rov.orbiting_group_type_id = (1,2,3,4) AND rov.orbiting_group_indice_id = 1

其中(1,2,3,4)是4个不同的SELECT语句,相应地具有(1,2,3,4)值。在下一行中,我将更改rov.orbiting_group_indice_id = 2,并希望使用相同的rov.orbiting_group_type_id值(1,2,3,4)。

我是SQL新手,我想问一下,如何用那个valeus呈现表格?
因此它应该类似于column1-value,column2-value,column3-value,column4-value。

谢谢!

更新:我想要以下内容:

SELECT 
    (SELECT
        SUM(val)
    FROM
        report_orbiting_vals rov
    WHERE
        rov.orbiting_group_type_id = 1
    AND rov.orbiting_group_indice_id = 1) as colOne,
(SELECT
        SUM(val)
    FROM
        report_orbiting_vals rov
    WHERE
        rov.orbiting_group_type_id = 1
    AND rov.orbiting_group_indice_id = 2) as colTwo,
(SELECT
        SUM(val)
    FROM
        report_orbiting_vals rov
    WHERE
        rov.orbiting_group_type_id = 1
    AND rov.orbiting_group_indice_id = 3) as colThree,
(SELECT
        SUM(val)
    FROM
        report_orbiting_vals rov
    WHERE
        rov.orbiting_group_type_id = 1
    AND rov.orbiting_group_indice_id = 4) as colFourth

不幸的是,上面的代码不起作用,会产生错误,但我希望你能理解我现在想要的东西。

更新(2):

我尝试了下面提供的2个解决方案,第一个:

SELECT
(SELECT
 SUM(val)
 FROM
 report_orbitings rov
 WHERE
 rov.orbiting_group_type_id = 1
 AND rov.orbiting_group_indice_id = 1) as colOne,
(SELECT
 SUM(val)
 FROM
 report_orbitings rov
 WHERE
 rov.orbiting_group_type_id = 1
 AND rov.orbiting_group_indice_id = 2) as colTwo,
(SELECT
 SUM(val)
 FROM
 report_orbitings rov
 WHERE
 rov.orbiting_group_type_id = 1
 AND rov.orbiting_group_indice_id = 3) as colThree,
(SELECT
 SUM(val)
 FROM
 report_orbitings rov
 WHERE
 rov.orbiting_group_type_id = 1
 AND rov.orbiting_group_indice_id = 4) as colFourth
FROM report_orbitings rv

输出:

enter image description here

第二名:

SELECT SUM(CASE WHEN rov.orbiting_group_type_id = 1 THEN val ELSE 0 END) as type1_sum,
SUM(CASE WHEN rov.orbiting_group_type_id = 2 THEN val ELSE 0 END) as type2_sum,
SUM(CASE WHEN rov.orbiting_group_type_id = 3 THEN val ELSE 0 END) as type3_sum,
SUM(CASE WHEN rov.orbiting_group_type_id = 4 THEN val ELSE 0 END) as type4_sum
FROM report_orbitings rov
WHERE rov.orbiting_group_type_id in (1,2,3,4)
AND rov.orbiting_group_indice_id = 1

输出:

enter image description here

要明确我想承认,我的数据库还没有数据,只是结构。不过,我认为第二个代码工作,而第一个没有,因为它输出[Null]而不是什么。为什么这两个例子有区别?它应该产生相同的输出。

2 个答案:

答案 0 :(得分:4)

我不确定是否已关注,要么您需要它们的总和,那么您需要使用$(".dropdown").on("contentChange", function(){ //do something })

IN()

或者您希望每个列的总和位于不同的列中:

SELECT SUM (val) FROM report_orbiting_vals rov
WHERE rov.orbiting_group_type_id in (1,2,3,4) 
  AND rov.orbiting_group_indice_id = 1

答案 1 :(得分:1)

也许你需要这个:

SELECT 
  (SELECT SUM(val) FROM report_orbiting_vals rov
   WHERE rov.orbiting_group_type_id = rv.orbiting_group_type_id
   AND rov.orbiting_group_indice_id = 1) as colOne,
  (SELECT SUM(val) FROM report_orbiting_vals rov
   WHERE rov.orbiting_group_type_id = rv.orbiting_group_type_id
   AND rov.orbiting_group_indice_id = 2) as colTwo,
  (SELECT SUM(val) FROM report_orbiting_vals rov
   WHERE rov.orbiting_group_type_id = rv.orbiting_group_type_id
   AND rov.orbiting_group_indice_id = 3) as colThree,
  (SELECT SUM(val) FROM report_orbiting_vals rov
   WHERE rov.orbiting_group_type_id = rv.orbiting_group_type_id
  AND rov.orbiting_group_indice_id = 4) as colFourth,
  rv.orbiting_group_type_id
FROM report_orbiting_vals rv