帮我看一下这个SQL语句错在哪里?

shpyw 2004-10-10 02:18:09
sqlstr = "SELECT [gxm] FROM [laogai] WHERE gfg=" & fg & "and Not [gxm] IN (SELECT [gxm] FROM [laogai] IN " & "'" & txtmb.Text & "'" & ")"
作用就是查找源数据库laogai表中gfg字段为fg字符串变量的gxm值,但这些值又不是在txtmb.text所代表的数据库中的laogai表中。
一开始我没有加gfg=" & fg & "and 这个条件时,那个是成功的。
...全文
149 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbman2003 2004-10-11
这样:
"SELECT [gxm]
FROM [laogai]
WHERE gfg = '" & fg & "'
And [gxm] Not IN
(SELECT [gxm] FROM [laogai] IN '" & txtmb.Text & "')"

其中:gfg = '" & fg & "'  字段gfg为文本类型要有单引号,数字类型不用单引号
   txtmb.Text 是另一个(Access)数据库的路径名称
回复
饮水需思源 2004-10-11
and Not [gxm] IN ----> and [gxm] Not IN
回复
yiliao 2004-10-11
你的:
sqlstr = "SELECT [gxm] FROM [laogai] WHERE gfg=" & fg & "and Not [gxm] IN (SELECT [gxm] FROM [laogai] IN " & "'" & txtmb.Text & "'" & ")"

修改后的:
sqlstr = "SELECT [gxm] FROM [laogai] WHERE gfg='" & fg & "'and Not [gxm] IN (SELECT [gxm] FROM [laogai] IN " & "'" & txtmb.Text & "'" & ")"

原因:
在SQL语句中不管引用的字符串还是变量都要加单引号的.

回复
QWERT520 2004-10-11
Private Sub Cmd_Click()
sqlstr = "SELECT [gxm] FROM [laogai] WHERE gfg='" & fg & "'" _
& " and [gxm] Not IN (SELECT [gxm] FROM [laogai] where [gxm] IN ('" & Trim(txtmb.TexT) & "'))"

End sub
回复
Andy__Huang 2004-10-10
應該這樣寫,你少了一個where

sqlstr = "SELECT [gxm] FROM [laogai] WHERE gfg=" & fg & "and Not [gxm] IN (SELECT [gxm] FROM [laogai] where [gxm] IN " & "'" & txtmb.Text & "'" & ")"
回复
vbman2003 2004-10-10
试试:
SELECT [gxm]
FROM [laogai]
WHERE gfg='" & fg & "'
And Not [gxm] IN
(SELECT [gxm] FROM [laogai] IN "" & txtmb.Text & "" )
回复
shpyw 2004-10-10
很急的,大家快帮我一下啊。
回复
shpyw 2004-10-10
好像不行,字段类型都是文本啊。fg是字符串。
另外问一下,假设一个表里面的所有gfg字段的值都是一样的
那我可不可以给fg字符串这样给它负值
select * from laogai
fg=rn.fields("gfg")
回复
baofucun2002 2004-10-10
gfg='"& fg &"'and
回复
描述信息太少,字段类型都不知道。你先试着把 gfg=" & fg & "and 改成 gfg='" & fg & "'and 试试看,不行就得多提供些信息才好检查。
回复
michael2002168 2004-10-10
"SELECT [gxm] FROM [laogai] WHERE gfg='" & fg & "' and Not [gxm] IN (SELECT [gxm] FROM [laogai] IN " & "'" & txtmb.Text & "'" & ")"
回复
GGL123 2004-10-10
gfg=" & fg & " and
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告