如何从 mysql 查询中存储对象 max(date) 和 min(date)

Posted

技术标签:

【中文标题】如何从 mysql 查询中存储对象 max(date) 和 min(date)【英文标题】:How to store objects max(date) and min(date) from mysql query 【发布时间】:2019-08-30 12:24:45 【问题描述】:

我是 C# 的半新手,我从未存储过 mysql 查询中的对象。我已经声明了 2 个日期时间变量:

DateTime date_min
DateTime date_max

我想从下面的查询中存储 max(date) 和 min(date)

string query1 = "SELECT MIN(order_status.BEGIN_DATE) AS 'BEGIN DATE', MAX(order_status.END_DATE) AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";

点赞:max(date) = MAX(order_status.END_DATE)

min(date) = MIN(order_status.BEGIN_DATE)

我想在这个循环中使用:

    for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
                 
        string query2 = "SELECT COUNT(leave.ID_PRACOWNIKA) AS 'NUMBER OF WORKERS ON LEAVE' FROM project1.leave WHERE DATE(leave.BEGIN_DATE) <= '"+date+"' AND DATE(leave.END_DATE) >= '"+date+"';";
        string query3 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ALL ORDERS IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"');";
        string query4 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ORDER IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER  = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"') AND orders.NUMBER_ORDER = 'TEST';";
    

谁能告诉我应该写什么样的代码行?也许 cmd.Parameters 就足够了吗?感谢您的帮助。

这是我的代码:

 public partial class GenerateChartsOfOrders : Form
    
        DateTime date_min;
        DateTime date_max;
        public GenerateChartsOfOrders()
        
            InitializeComponent();
        
        public void loaddata2()
        
string query1 = "SELECT MIN(order_status.BEGIN_DATE) AS 'BEGIN DATE', MAX(order_status.END_DATE) AS 'END DATE' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE orders.NUMBER_ORDER = 'TEST';";

            for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
                         
                string query2 = "SELECT COUNT(leave.ID_PRACOWNIKA) AS 'NUMBER OF WORKERS ON LEAVE' FROM project1.leave WHERE DATE(leave.BEGIN_DATE) <= '"+date+"' AND DATE(leave.END_DATE) >= '"+date+"';";
                string query3 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ALL ORDERS IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"');";
                string query4 = "SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s') AS 'TIME OF ORDER IN DAY' FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER  = order_status.ID_ORDER WHERE (DATE(order_status.BEGIN_DATE) = '"+date+"' AND DATE(order_status.END_DATE) = '"+date+"') AND orders.NUMBER_ORDER = 'TEST';";
            
        
    

【问题讨论】:

【参考方案1】:

您可以使用ExecuteReader 检索如下值,

using (MySqlConnection connection = new MySqlConnection(connectionString))

using(var command = new MySqlCommand(query1, con))

    using(var reader = command.ExecuteReader())
    
       while (reader.Read())
       
        date_min = Convert.ToDateTime(reader[0]);
        date_max = Convert.ToDateTime(reader[1]);
       
    


希望对你有帮助。

【讨论】:

好的,但是你写的这个我应该添加到我的代码中还是在我的代码之后?

以上是关于如何从 mysql 查询中存储对象 max(date) 和 min(date)的主要内容,如果未能解决你的问题,请参考以下文章

如何将varbinary dat插入sqlserverce

如何从具有 10k 条记录的 mysql 数据库中快速检索数据

如何在 MySQL 中查询具有对象数组属性的对象?

mysql中如何查看最大连接数max

查询存储在 MySQL 中的多级 JSON 对象

跨 2 个表的 Mysql SQL 查询 - 不知道如何正确执行