【问题标题】:MySQL: how can i get the latest datetime together with the other values?MySQL:我怎样才能得到最新的日期时间和其他值?
【发布时间】:2015-03-12 13:35:21
【问题描述】:
MySqlConnection con = new MySqlConnection("connection...");
MySqlCommand cmd = new MySqlCommand("Select Year,Newsales,Beginninginventory,Purchases,Goodsforsales,Endinventory,Costofsales,GrossIncome,Salesandwages,sss,Rental,Postage,Lightandwater,Freight,Fuel,Officesupplies,Taxesandlicenses,Representation,Depreciation,Transportation,Repairs,Miscellaneous,Totaloperatingexpense,Netincomebeforetaxes,Provisions,Netincomefinal from revenue where Year = select max(Year)", con);
MySqlDataReader reader;
try
{
    con.Open();
    reader = cmd.ExecuteReader();
    while(reader.Read())
    {
        this.chart1.Series["Sales"].Points.AddXY(reader.GetString("Sales"),reader.GetInt32("NewSales"));
        this.chart1.Series["Sales"].Points.AddXY(reader.GetString("Cost"), reader.GetInt32("Costofsales"));
        this.chart1.Series["Sales"].Points.AddXY(reader.GetString("Gross"), reader.GetInt32("GrossIncome"));
        this.chart1.Series["Sales"].Points.AddXY(reader.GetString("TotalOE"), reader.GetInt32("Totaloperatingexpense"));
        this.chart1.Series["Sales"].Points.AddXY(reader.GetString("NIBT"), reader.GetInt32("Netincomebeforetaxes"));
        this.chart1.Series["Sales"].Points.AddXY(reader.GetString("NetIncome"), reader.GetInt32("Netincomefinal"));
    }
}
catch(Exception t)
{
    MessageBox.Show(t.Message);
}

在我的收入表中(示例): 年份 Newsales Begginginventory ...... 净收入 2015-03-10 83390 30000 20302.08
2015-03-11 87680 20000 18680 2015-03-12 105750 30000 61415.25

期望的输出: 2015-03-12 105750 30000 61415.25

【问题讨论】:

  • 你当前的 SQL 语句在什么方面没有做你想做的事?
  • select = max() 错误。
  • 我现在明白了。您需要在之后添加from revenue 并将() 放在子查询周围。请注意,如果您编辑 SQL 使其不是一大行文本,人们会更容易看到问题。

标签: c# mysql


【解决方案1】:

问题是select max(Year) 本身不是一个有效的子查询。所以改变你的 SQL 从

Select
    Year,
    Newsales,
    Beginninginventory,
    Purchases,
    Goodsforsales,
    Endinventory,
    Costofsales,
    GrossIncome,
    Salesandwages,
    sss,
    Rental,
    Postage,
    Lightandwater,
    Freight,
    Fuel,
    Officesupplies,
    Taxesandlicenses,
    Representation,
    Depreciation,
    Transportation,
    Repairs,
    Miscellaneous,
    Totaloperatingexpense,
    Netincomebeforetaxes,
    Provisions,
    Netincomefinal 
from revenue 
where Year = select max(Year)

到

Select *
from revenue 
where Year = (select max(Year) from revenue)

请注意,如果您想要所有列,则无需列出每一列。只需使用*。如果列数比您列出的多,请返回列出它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2018-03-16
    • 2014-09-06
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多