获取前一行,其中列等于相同的值ms访问

时间:2014-07-14 04:51:54

标签: c# sql ms-access

是否可以获取特定列等于特定值的前一行。

我尝试过的例子。

        //Prev ID
        int numval = Convert.ToInt32(pointid.Text);
        int id = numval;
        id--;
        //textBox1.Text = id.ToString();
        //int prevval = Convert.ToInt32(textBox1.Text);

        //Point Type ID
        int catid = Convert.ToInt32(catId.Text);

        cmdGet.CommandText = "SELECT * FROM Points WHERE points_type_id = @catid and point_id = @prevval";
        cmdGet.Parameters.AddWithValue("@prevval", id);
        cmdGet.Parameters.AddWithValue("@catid", catid);
        OleDbDataReader reader = cmdGet.ExecuteReader();


        while (reader.Read())
        {
            pointlbl.Text = reader["point"].ToString();
            //To be used for next previous buttons to only get results from the same catid
            //int type = reader.GetInt32(reader.GetOrdinal("point_id"));
            catId.Text = reader["points_type_id"].ToString();
            webBrowser1.DocumentText = reader["Description"].ToString();
        }

        conGet.Close();

所以我有点点,点击它们会在id列中显示该点id匹配的数据。然后我有前一个和下一个按钮,如果point_type_id匹配相同的categoryid,我想显示上一行或下一行。

这不起作用。

以下是表格的示例。

ID | Point | point_type_id
1      A          1
2      B          1
3      C          2
4      D          2
5      E          1

所以说我开始第五行ID = 5,其中point_type_id = 1,我想得到前一行,其中point_type_id = 1,这将是第三行ID = 3

1 个答案:

答案 0 :(得分:0)

比我想象的要容易......

        cmdGet.CommandText = "SELECT * FROM Points WHERE point_id < @curval and points_type_id = @catid";
        cmdGet.Parameters.AddWithValue("@curval", numval);
        cmdGet.Parameters.AddWithValue("@catid", catid);