两表查询问题

cs_lx_sc 2008-07-18 03:21:47
表1:
column1 cai
d1 100
d2 101
d3 102
d4 103


表2:
column2 cai
e1 100
e2 101

现在如何做一个连接,能得到这样如: column1 cai column2
d1 100 e1
d2 101 e2
d3 102
d4 103 的结果,并且可以对每个字段进行查询

...全文
42 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2008-07-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cs_lx_sc 的回复:]
我在查询事件里写

ff = ""
If Me.Name <> Null Then
ff = "[tb1].name='" & Me.Name & "'"
End If

If Me.age <> Null Then
ff = ff & " and [tb2].age='" & Me.age & "' "
End If

'dd = "select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai where " & ff
DoCmd.RunSQL "select * into [temp] from [tb1] a left join [tb2] b on a.ID=b.ID where " & ff
Me.window.SourceObject = "表.temp"

为…
[/Quote]
ff=" true "
or
在每一个IF中加入判断LEN(FF)>0
cs_lx_sc 2008-07-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cs_lx_sc 的回复:]
连接我知道,用左联就行能出来 column1 cai column2
d1 100 e1
d2 101 e2
d3 102
d4 103 的结果,

但下来要实现对这三个字段的查询功能该怎么实现?
[/Quote]

现在问题是怎么样生成这个效果的一张表呢?
dd="select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai where " & ff
set qq=currentdb.openrecordset(dd)
生成了recordset下来怎么做?
ccssddnnhelp 2008-07-18
  • 打赏
  • 举报
回复

推荐你看一下这些帮助


ACCESS 2003 Help 下载
VBA参考 下载
Access VBA 参考下载
Access Northwind.mdb 下载
JET SQL HELP 下载
SQL 1992
DAO 3.6 Manual 下载
ActiveX Data Objects 2.5 Reference 下载
数据库系统概论PPT.
==== ====

.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
ccssddnnhelp 2008-07-18
  • 打赏
  • 举报
回复

其实你自己用debug看一下
"select * into [temp] from [tb1] a left join [tb2] b on a.ID=b.ID where " & ff

是什么就明白了,你的 Me.Name 或 Me.age 有为空的,另外你的 [tb2].age是什么型,如果数字型则不要用单引号.
==== ====

.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
ccssddnnhelp 2008-07-18
  • 打赏
  • 举报
回复

ff = "1=1"
If Me.Name <> Null Then
ff = ff & " and [tb1].name='" & Me.Name & "'"
End If

If Me.age <> Null Then
ff = ff & " and [tb2].age='" & Me.age & "' "
End If

DoCmd.RunSQL "select * into [temp] from [tb1] a left join [tb2] b on a.ID=b.ID where " & ff
Me.window.SourceObject = "表.temp"
.
==== ====

.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
cs_lx_sc 2008-07-18
  • 打赏
  • 举报
回复
我在查询事件里写

ff = ""
If Me.Name <> Null Then
ff = "[tb1].name='" & Me.Name & "'"
End If

If Me.age <> Null Then
ff = ff & " and [tb2].age='" & Me.age & "' "
End If

'dd = "select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai where " & ff
DoCmd.RunSQL "select * into [temp] from [tb1] a left join [tb2] b on a.ID=b.ID where " & ff
Me.window.SourceObject = "表.temp"

为什么提示where有错?麻烦你
WWWWA 2008-07-18
  • 打赏
  • 举报
回复
or
dd="select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai "
set qq=currentdb.openrecordset(dd,dbOpenDynaset)
ff.Filter = ff
WWWWA 2008-07-18
  • 打赏
  • 举报
回复
用VBA代码进行判断
ff=""
if 文本框1<>' ' then
ff="column1='" & 文本框1 & "'"
end if

if 文本框2<>' ' then
ff=ff & " and column2='" & 文本框2 & "' "
end if

if 文本框3<>' ' then
ff=ff & "and cai='" & 文本框3 & "'"
end if

dd="select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai where " & ff
set qq=currentdb.openrecordset(dd)



WWWWA 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cs_lx_sc 的回复:]
就是在窗体里画三个框,可以分别输入内容对他们进行查询

比如输入d1

就出来 d1 100 e1这样的功能
[/Quote]
将上述代码存为查询1
select * from 查询1 where column1='d1'
or
select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai
where column1='d1'
WWWWA 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cs_lx_sc 的回复:]
~~~~~~~~~~~~~~~~~~~~~~``
[/Quote]
呵呵,要将自己的意思言简意赅地表达出来哦
cs_lx_sc 2008-07-18
  • 打赏
  • 举报
回复
就是在窗体里画三个框,可以分别输入内容对他们进行查询

比如输入d1

就出来 d1 100 e1这样的功能
cs_lx_sc 2008-07-18
  • 打赏
  • 举报
回复
~~~~~~~~~~~~~~~~~~~~~~``
WWWWA 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cs_lx_sc 的回复:]

但下来要实现对这三个字段的查询功能该怎么实现?
[/Quote]

没有理解是什么意思、,举例说明要求
cs_lx_sc 2008-07-18
  • 打赏
  • 举报
回复
连接我知道,用左联就行能出来 column1 cai column2
d1 100 e1
d2 101 e2
d3 102
d4 103 的结果,

但下来要实现对这三个字段的查询功能该怎么实现?
WWWWA 2008-07-18
  • 打赏
  • 举报
回复
并且可以对每个字段进行查询
什么意思?
WWWWA 2008-07-18
  • 打赏
  • 举报
回复
以B1为准?
select a.*,b.column2 from b1 a left join b2 b on a.xai=b.cai

7,714

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点。
社区管理员
  • Access
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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