将总月收入纳入vb.net的图表中

时间:2017-10-26 12:34:18

标签: mysql vb.net


我提出了一个查询,以获得一年中每个月的总收入 我是vb.net和MySQL的新手。如果有人知道更短的查询,请给出相同的结果。
我想把图表放在X表示月份,Y表示总收入。
我的查询:

query = "SELECT  (
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '1'
)as January,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '2'
) AS February,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '3'
)as March,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '4'
) AS April,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '5'
)as May,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '6'
) AS June,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '7'
)as July,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '8'
) AS August,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '9'
)as September,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '10'
) AS October,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '11'
)as November,
(
SELECT SUM(Amount_income_table)
FROM   bacci.income_table
where  MONTH(Date_income_table) = '12'
) AS December;"

图表的代码是:

    Comand = New MySqlCommand(query, connection)
            READER = Comand.ExecuteReader
            While READER.Read
                ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("Date_income_table"), READER.GetInt32("Amount_income_table"))

            End While

我正在使用MySQL数据库和Visual Basic 2017。

1 个答案:

答案 0 :(得分:2)

您可以使用MONTHNAME函数直接获取月份和组的名称。

SELECT SUM(amt), MONTHNAME(amt_date) 
FROM income_table
GROUP BY MONTHNAME(amt_date);

类似的东西。

http://sqlfiddle.com/#!9/62f4ba/1

由于某种原因,小提琴链接不起作用,这里是整个代码

 CREATE TABLE income_table (amt INT, amt_date DATE);

  INSERT INTO income_table
    VALUES (12500,'2017-01-01');
      INSERT INTO income_table
    VALUES (2500,'2017-01-10');

    INSERT INTO income_table
    VALUES (12500,'2017-02-01');

      INSERT INTO income_table
    VALUES (3700,'2017-02-08');

    INSERT INTO income_table
    VALUES (12500,'2017-03-01');
    INSERT INTO income_table
    VALUES (12500,'2017-04-01');
    INSERT INTO income_table
    VALUES (12500,'2017-05-01');
    INSERT INTO income_table
    VALUES (12500,'2017-06-01');

在小提琴中看到。