zoukankan      html  css  js  c++  java
  • SQL语句的运用

    1.SQL语句的随机函数:

      语句格式:select top 10 * from tb_test order by newid();

      意思:表示从tb_test表中随机抽取10条记录。

      附上一个用C#编写的方法实例:  

    代码
    public DataSet select_test(ComboBox mycom1, ComboBox mycom2, TextBox txt1) //随机检索一定数量的命题
    {
    conn.Find_data(
    "select top " + int.Parse(txt1.Text.ToString()) + " * from tb_test where itemtype = '" + mycom1.Text + "' and value = '" + mycom2.Text + "' order by newid()");
    if (conn.D_S().Tables[0].Rows.Count == 0)
    {
    MessageBox.Show(
    "找不到相应数据", "提示");
    return null;
    }
    else
    {
    return conn.D_S();
    }
    }

    2.寻找最大值(可能出现0的情况)

      语句格式:select  case when max(testpaper_ID) is null then 0 else max(testpaper_ID) end from tb_testpaper

      意思:表示从tb_testpaper表中找寻testpaper_ID的最大值,没有的话就输出0,有的话找到之后退出。

      附上一个用C#编写的方法实例: 

      

    代码
    public int select_testpaperID() //查询试卷表中的最大值
    {
    int max = conn.find_max("select case when max( testpaper_ID ) is null then 0 else max( testpaper_ID ) end from tb_testpaper"); //找查试卷表中的最大值
    if (max == 0)
    {
    MessageBox.Show(
    "找不到相应数据", "提示");
    return 0;
    }
    else
    {
    return max;
    }
    }

     3.SQL的嵌套查询

      语句格式:select subject from tb_test where title_ID in (select title_ID from tem_testpaper where tem_ID = 1)

      意思:先从tem_testpaper表中找到tem_ID = 1的title_ID,然后再根据这个title_ID,从tb_test表中从找到相应的subject。

      附上一个用C#编写的方法实例: 

    代码
    public void give_obj() //客观题评分
    {
    int max_title_ID = conn.find_max("select max(tem_ID) from tem_testpaper,tb_test where tem_testpaper.title_ID = tb_test.title_ID and itemtype <> '主观题'");
    conn.Find_data(
    "select tem_testpaper.title_ID,tem_result,result,value from tem_testpaper,tb_test where tem_testpaper.title_ID = tb_test.title_ID and itemtype <> '主观题'");
    if (conn.D_S().Tables[0].Rows.Count == 0)
    {
    MessageBox.Show(
    "找不到相应数据", "提示");
    }
    else
    {
    for (int i = 0; i < max_title_ID; i++)
    {
    if (conn.D_S().Tables[0].Rows[i][1].ToString().Trim() == conn.D_S().Tables[0].Rows[i][2].ToString().Trim())
    {
    conn.executeSQL(
    "update tem_testpaper set tem_value = " + conn.D_S().Tables[0].Rows[i][3].ToString() + " where title_ID = " + conn.D_S().Tables[0].Rows[i][0].ToString());
    //MessageBox.Show("插入成功", "提示");
    }
    else
    {
    conn.executeSQL(
    "update tem_testpaper set tem_value = ' 0 ' where title_ID = " + conn.D_S().Tables[0].Rows[i][0].ToString());
    }
    }
    }
    }

       

         4.表的外连接

      语句格式:select max(tem_ID) from tem_testpaper,tb_test where tem_testpaper.title_ID = tb_test.title_ID and itemtype <> '主观题'

      意思:把tem_testpaper表与tb_test表连接起来,查询tem_ID的最大值 

        附上一个用C#编写的方法实例:

    代码
    public DataSet exam_subject_result(DataGridView mydataview2) //通过datagridview的id来显示题面,答案
    {
    conn.Find_data(
    "select subject,itemtype from tb_test where title_ID in (select title_ID from tem_testpaper where tem_ID = " + mydataview2.CurrentRow.Cells[0].Value + ")");
    if (conn.D_S().Tables[0].Rows.Count == 0)
    {
    MessageBox.Show(
    "找不到相应数据", "提示");
    return null;
    }
    else
    {
    return conn.D_S();
    }
    }

     

  • 相关阅读:
    去除vue-router 链接上的#号
    input 实现调用本地摄像头 实现拍照 和拍视频
    vue-cli 安装sass 和 font-awesome 笔记
    linux下alias命令详解
    linux下source命令的基本功能
    在Linux命令行窗口中,怎么向上翻页?
    Android内存监测工具使用
    Activity的Theme主题风格
    ListView中CheckBox错乱解决
    绘图之Canvas学习
  • 原文地址:https://www.cnblogs.com/guolebin7/p/1741550.html
Copyright © 2011-2022 走看看