请问基于三张表格的组合查询如何实现?

jhxuhua 2004-09-07 06:27:30
数据库中三张表格1,2,3想查询:三张表中都有的,1有、2和3没有的等等,这样组合共有8种情况。
界面上针对三张表格设计六个单选框,分别代表1的有和无,2的有和无,3的有和无,对应RadioButton1~6。
再放一个datagrid在查询完后将表格中的内容显示出来,比如选择1有,2、3没有的情况,将符合条件的表中的结果显示出来。
以下是设定简单的情况:先考虑两张表格,但是出不来查询结果,请指教!

谢谢!!!

If RadioButton1.Checked = True And RadioButton3.Checked = True Then
MySelectCommand.CommandText = "Select * from d1,g1 Where d1.a1=g1.a2"
MySelectCommand.Connection = MyConn
MyDataAdapter.SelectCommand = MySelectCommand
Dim MyDataSet As New DataSet
DataGrid1.CaptionText = "公司基本资料"
DataGrid1.DataSource = MyDataSet
Else
If RadioButton1.Checked = True And RadioButton4.Checked = True Then

MySelectCommand.CommandText = "Select * from d1,g1 Where d1.a1<>g1.a2"
MySelectCommand.Connection = MyConn
MyDataAdapter.SelectCommand = MySelectCommand
Dim MyDataSet As New DataSet
DataGrid1.CaptionText = "公司基本资料"
...全文
142 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
AntingZ 2004-09-08
  • 打赏
  • 举报
回复
or:

str = "select id from (select id from 表1 union select id from 表2 union select id from 表3) a " & _
" where a.id {0} in (select id from 表1 ) " & _
" and a.id {1} in (select id from 表2 ) " & _
" and a.id {2} in (select id from 表3 ) "
jhxuhua 2004-09-08
  • 打赏
  • 举报
回复
我试了一下,三个表都有的情况,结果很快就有,但是,只要查询如一个表没有的等情况,就出不来结果,机器很慢,这种查询复杂度有这么高吗?
每个表中大概有2万条记录.

谢谢!!
jhxuhua 2004-09-08
  • 打赏
  • 举报
回复
不好意思,本人刚开始学不久,不少还不太明白,能否再讲具体些呢?
非常谢谢您前面的指导!!

wangsaokui 2004-09-08
  • 打赏
  • 举报
回复
建立只包括id的视图,选取表A有,表B,C没有的,或者B有,A,C没有的......再查询视图会快n倍,将id设置为index
jhxuhua 2004-09-08
  • 打赏
  • 举报
回复
经过了很长时间,查询的结果终于出来了,不过感觉这么长的时间,使用上不太能接受,
请问有否查询的计算复杂度优化些的算法,能够提高查询的速度呢?

谢谢指点!!
AntingZ 2004-09-07
  • 打赏
  • 举报
回复
你不用checkbox也行,如果表1中有的话把ISExists1="", 如果没有则IsExists="not"就行
AntingZ 2004-09-07
  • 打赏
  • 举报
回复
try:

Dim strSql As String
Dim str As String
str = "select id from (select id from 表1 union select id from 表2 union select id from 表3) a " & _
" where {0} exists (select 1 from 表1 where id=a.id) " & _
" and {1} exists (select 1 from 表2 where id=a.id) " & _
" and {2} exists (select 1 from 表3 where id=a.id) "

Dim IsExists1, IsExists2, IsExists3 As String
IsExists1 = IIf(Me.CheckBox1.Checked, "", "not")
IsExists2 = IIf(Me.CheckBox2.Checked, "", "not")
IsExists3 = IIf(Me.CheckBox3.Checked, "", "not")

strSql = String.Format(str, IsExists1, IsExists2, IsExists3)

MySelectCommand.CommandText=strSql

以上代码没有测试过,你测试一下,
checkbox1,2,3分别代表是否存在于表1,2,3中
AntingZ 2004-09-07
  • 打赏
  • 举报
回复
不如放3个复选框,分别代表有没有1,2,3
AntingZ 2004-09-07
  • 打赏
  • 举报
回复
这样组合共有8种情况。
界面上针对三张表格设计六个单选框

-----------
还有2种情况怎么办?
LiloZhu 2004-09-07
  • 打赏
  • 举报
回复
同上
Eddie005 2004-09-07
  • 打赏
  • 举报
回复
同意
wangsaokui 2004-09-07
  • 打赏
  • 举报
回复
最好形成相应的字符串,001,100,110等等

select case str
case "100"
sqlstr="select * from d1 where a1 not in (select a2 from g1 union select 第三个表的列 from 第三个表)" '因为我不知道名字
case "010"
....
case "110"
sqlstr = "Select * from d1,g1 where d1.a1=g1.a2 and d1.a1 not in (select 第三个表的列 from 第三个表)"
....
end select

MySelectCommand.CommandText =sqlstr
MyDataAdapter.SelectCommand = MySelectCommand
Dim MyDataSet As New DataSet
DataGrid1.CaptionText = "公司基本资料"
DataGrid1.DataSource = MyDataSet

这样写好些

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧