gpt4 book ai didi

mysql - 复杂日期查询SQL

转载 作者:行者123 更新时间:2023-11-29 22:27:58 24 4
gpt4 key购买 nike

我想向此代码添加一个日期过滤器:

var db = Database.Open("Tool") ;
var selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders Group by Nom ";

var selectedData = db.Query(selectCommand);
var grid = new WebGrid(source: selectedData, defaultSort: "Column1", rowsPerPage:200);
grid.SortDirection = SortDirection.Descending;

将从输入中选取日期(type=date)...

我知道要添加的代码如下所示:

Where Date between date1 and date2

我只是不知道如何将其插入到上面的代码中

谁能帮帮我

谢谢

好的,代码现在看起来像这样:

var db = Database.Open("Tool") ;
var selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders GROUP BY Nom";
var selectedData = db.Query(selectCommand);

if(!Request.QueryString["Date1"].IsEmpty() || !Request.QueryString["Date2"].IsEmpty()) {
var date1 = Request["Date1"];
var date2 = Request["Date2"];
selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders WHERE Date BETWEEN " + date1 + " AND " + date2 + " GROUP BY Nom";
selectedData = db.Query(selectCommand);
}
var grid = new WebGrid(source: selectedData, defaultSort: "Column1", rowsPerPage:200);
grid.SortDirection = SortDirection.Descending;

我在尝试填充这两个输入时遇到错误:Échec de la conversion des données。 [ OLE DB 状态值(如果已知)= 2 ]

最佳答案

Where 子句应添加到 selectCommand 查询中,如下所示:

SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders 
WHERE Date BETWEEN date1 and date2 GROUP BY Nom

带有输入表单字段的 PFB 查询:

var selectCommand = "SELECT Nom, COUNT(Nom) FROM DTool2 AS NumberOfOrders WHERE Date BETWEEN '" + date1  + "' AND '" + date2  + '" GROUP BY Nom";

关于mysql - 复杂日期查询SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com