上海山樟木厂家:sql查询方法!求指教

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 12:43:38
比方说一个表只有一个字段,int类型,现在有5条记录,分别是1,3,4,5,9.
现在要查询出他们空缺的部分如何查询,现在是需要查询出从1开始循序往后的字段不连续的一个数.
比如说现在第一个不连续的是2,所以查询出2.
如果记录不确定有多少条的情况下.如何做?
有asp.net的例子吗?最好是c#的

在代码文件中:
public void OnbtFindClick(Object sender,EventArgs e)
{
string connectString = "server=(local);user id=sa;initial catalog=testnumber;password=";
SqlConnection MyConnection = new SqlConnection(connectString);
MyConnection.Open();
string sqlstring = "select * from test_table";

SqlDataAdapter MyCommand = new SqlDataAdapter(sqlstring,MyConnection);
DataSet ds = new DataSet();
MyCommand.Fill(ds,"test_table");

DataTable dt = ds.Tables["test_table"];

int iFind = Convert.ToInt32(dt.Rows[0]["column"].ToString());
int i = 1;
for (;i < dt.Rows.Count;i++)
{
if(iFind +i != Convert.ToInt32(dt.Rows[i]["column"].ToString()))
break;
}
Response.Write(iFind+i);
}
在网页设计文件中,增加一个button:
<asp:Button ID="btFind" Text="Press Me!" OnClick="OnbtFindClick" runat="server"></asp:Button>
注:testnumber为数据库名字,test_table为表名,column为int类型的列。vs.net2003下测试成功!

select * from 表 where 列 not in(1,3,4,5,9)

没学过asp.net 不过估计语法都差不多
你可以改成这样
strsql="select * from 表 where 列 not in("+int(变量1)+","
+int(变量2)+")"
int是我自己猜的 不知道asp.net 用什么做类型转换
大概就是这样 估计可以实现

这个用编程自己搞定,我用ASP你看看.
temp=rs("字段") '刚开始rs("字段")为1,赋给一个变量temp,
while not rs.eof
if rs("字段") <> temp+1 then '通常情况下rs("字段")为2,此时却为3
for i=temp+1 to rs("字段")-1'输出中间间隔的数字
response.write(i)
next
end if
rs.movenext
if not rs.eof then
temp=rs("字段") '然后逐个rs("字段")赋给变量temp,
end if
wend