[英]C#: SQL “Select Top..” query does not return any rows?
I need to: 我需要:
1) Filter Rows by "NodeParent" (which I provide) 1)按“ NodeParent”(我提供的)过滤行
2) Sort rows by "Time" and get the most recent entry 2)按“时间”对行进行排序,并获取最新的条目
Here's what I wrote: 这是我写的:
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT TOP 1 NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
nodeid_previous = rdr["NodeID"].ToString();
break;
}
rdr.Close();
}
else
{
//so on and so forth..
}
}
This however doesn't return any results even though I HAVE rows which abide by these conditions. 但是,即使我有遵守这些条件的行,也不会返回任何结果。 Is the query correct folks?
查询是否正确? :)
:)
Before 之前
SqlCommand cmd = new SqlCommand("SELECT TOP 1 NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC", con);
After - Limit is added to the end of your query, rather than in the beginning of you select list like MS SQL Server's Top command 之后 -将限制添加到查询的末尾,而不是像MS SQL Server的“顶部”命令那样在列表的开头选择列表
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);
You have to use "Limit 1" in the end. 最后,您必须使用“限制1”。 There is no "TOP 1" in MYSQL.
MYSQL中没有“ TOP 1”。 I guess it is SQLServer command.
我猜这是SQLServer命令。
Do: 做:
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.