我需要:
1)通过" NodeParent"过滤行(我提供的)
2)按行排序"时间"并获取最新条目
这是我写的:
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..
}
}
即使我有符合这些条件的行,但这并不会返回任何结果。查询是否正确的人? :)
答案 0 :(得分:1)
<强>之前强>
SqlCommand cmd = new SqlCommand("SELECT TOP 1 NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC", con);
之后 - 将限制添加到查询结尾,而不是在开头选择列表,如MS SQL Server的Top命令
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);
答案 1 :(得分:0)
你必须使用&#34;限制1&#34;到底。没有&#34; TOP 1&#34;在MYSQL中。我想这是SQLServer命令。
执行:
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);