【问题标题】:How to display random 10 record from database?如何显示数据库中的随机 10 条记录?
【发布时间】:2013-11-02 16:57:27
【问题描述】:

如何从数据库中随机显示 10 个问题?我如何使用数据库检查答案是否正确?

        db.command(true, "SELECT * FROM question WHERE Age_group='" +category + "'");
        foreach (DataRow item in db.result.Rows)
        {
            question_list.Add (Convert.ToInt32(item["id"]));
        }

        for (int i = 0; i < max_question; i++)
        {
            int index = ran.Next(question_list.Count);
            question_choose.Add(question_list[index]);
            question_list.Remove(question_list[index]);
        }

【问题讨论】:

标签: c# sql database random-sample


【解决方案1】:
select top 10 * from table order by newid()

另外,请参阅"order by newid()" - how does it work?

【讨论】:

  • 是的,它的工作。我如何随机选择属于我选择的年龄类别的 10 个问题?
  • 你可以尝试 select top 10 * from table where age>10 order by newid()
  • 那不行,我试试 SELECT top 15 * FROM question where age_group='" +category+ "order by newid()" 但还是不行
  • 在您的示例中,您在 category 之前有一个额外的单引号 -> age_group = ' " +category....
  • ya, 在表格中,我会放 3 种类别,即 6-10、11-15、16-20 让用户选择,最后我怎么知道年龄类别由用户选择并随机向他们提问?
猜你喜欢
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 2019-03-25
  • 2012-04-09
相关资源
最近更新 更多