当特定字段为空时如何获取行数

发布于 2024-12-02 22:28:27 字数 751 浏览 1 评论 0原文

RowID       TimeReceived    TimeRead    tbl_user_UserID     tbl_message_MsgID

  5     2011-09-06 11:16:20   NULL               2                1
  6     2011-09-06 11:17:04   NULL               3                1
  7     2011-09-06 11:17:19   NULL               100              1

这是我的表,

 command = new MySqlCommand("SELECT COUNT(Distinct RowID) FROM tbl_usermessage WHERE TimeRead= NULL AND tbl_message_MsgID=@Value1", connectionString);
         command.Parameters.Add("@value1", MySqlDbType.Int32, 25);
         command.Parameters["@value1"].Value = MessageID;
         int nnnID = Convert.ToInt32(command.ExecuteScalar());

我想计算时间读取为空的行,它给我 0 作为输出,而它应该是 3。我哪里出错了。我已将时间读取的默认值设置为空。

RowID       TimeReceived    TimeRead    tbl_user_UserID     tbl_message_MsgID

  5     2011-09-06 11:16:20   NULL               2                1
  6     2011-09-06 11:17:04   NULL               3                1
  7     2011-09-06 11:17:19   NULL               100              1

This is my table

 command = new MySqlCommand("SELECT COUNT(Distinct RowID) FROM tbl_usermessage WHERE TimeRead= NULL AND tbl_message_MsgID=@Value1", connectionString);
         command.Parameters.Add("@value1", MySqlDbType.Int32, 25);
         command.Parameters["@value1"].Value = MessageID;
         int nnnID = Convert.ToInt32(command.ExecuteScalar());

I want to count the row where time read is null, it gives me 0 as output where it should be 3. Where am I going wrong. i have set the default value for time read as null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有伤那来痛 2024-12-09 22:28:27

查询获取计数

select count(Distinct RowID) from table where TimeRead is null

Query for getting count

select count(Distinct RowID) from table where TimeRead is null
柠檬 2024-12-09 22:28:27

使用“is NULL”而不是“= NULL

SELECT COUNT(Distinct RowID) FROM tbl_usermessage WHERE TimeRead is NULL ...

同时检查您的参数@value1是否正确!

Use "is NULL" instead of "= NULL"

SELECT COUNT(Distinct RowID) FROM tbl_usermessage WHERE TimeRead is NULL ...

Also check if your parameter @value1 is correct!

白况 2024-12-09 22:28:27

你的SQL不正确。您可以使用 IS NULL 检查 NULL:

 command = new MySqlCommand("SELECT COUNT(Distinct RowID) FROM tbl_usermessage WHERE TimeRead IS NULL AND tbl_message_MsgID=@Value1", connectionString);
         command.Parameters.Add("@value1", MySqlDbType.Int32, 25);
         command.Parameters["@value1"].Value = MessageID;
         int nnnID = Convert.ToInt32(command.ExecuteScalar());

Your SQL is incorrect. You can check for NULL by using IS NULL:

 command = new MySqlCommand("SELECT COUNT(Distinct RowID) FROM tbl_usermessage WHERE TimeRead IS NULL AND tbl_message_MsgID=@Value1", connectionString);
         command.Parameters.Add("@value1", MySqlDbType.Int32, 25);
         command.Parameters["@value1"].Value = MessageID;
         int nnnID = Convert.ToInt32(command.ExecuteScalar());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文