无法检查两个时间字段(mysql、c#、windowsform应用程序)中是否存在时间

nukf8bse  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(275)

我在mysql数据库中有两个时间字段。我需要在添加新时间之前检查,我尝试这个代码。

private bool TimeFound()
{
    MySqlConnection cnn = new MySqlConnection(DB.GetCS("127.0.0.1", "3306",
        "db", "root", "password", "utf8"));
    string query = "SELECT time.From, time.To FROM db.time WHERE " +
        "(time.From, time.To) NOT IN (SELECT time.From, time.To FROM db.time " +
        "WHERE (time.From = @From AND time.To = @To";
    cnn.Open();
    MySqlCommand command = new MySqlCommand(query, cnn);
    command.Parameters.AddWithValue("@From", dateTimePicker1.Value.ToString("HH:mm:ss"));
    command.Parameters.AddWithValue("@To", dateTimePicker2.Value.ToString("HH:mm:ss"));
    object result = command.ExecuteScalar();
    if (result == null)
    {
        return false;
    }
    else
    {
        return true;
    }
}

每次我试图调用这个函数时,我都会在这一行出错。

object result = command.ExecuteScalar();

mysql.data.mysqlclient.mysqlexception:'您的sql语法有错误;请查看与mysql服务器版本对应的手册,以获取在第1行的“”附近使用的正确语法

ny6fqffe

ny6fqffe1#

感谢uuerdo的评论,在查询的最后,我不知道我怎么会错过这个。

webghufk

webghufk2#

正如我们已经讨论过的,您缺少两个右括号,但是您的查询似乎过于复杂。

SELECT time.From, time.To 
FROM db.time 
WHERE (time.From, time.To) NOT IN (
   SELECT time.From, time.To 
   FROM db.time 
   WHERE (time.From = @From AND time.To = @To)
)

不应该有任何不同于

SELECT time.From, time.To 
FROM db.time 
WHERE (time.From = @From AND time.To = @To)

表单的每个查询

SELECT x, y FROM a WHERE (x, y) IN (SELECT x, y FROM a WHERE x = x1 AND y = y1)

与相同

SELECT x, y FROM a WHERE x = x1 AND y = y1

相关问题