SQL select in 语句问题

max_shell 2010-03-02 12:09:43

Command.CommandText = "select Playfile.FileName from Playfile where Playfile.PID IN(select Playlist.PID from Playlist where Playlist.Name1 = '" & TextBox1.Text & "')"

这样说 表达式中的类型不匹配
可是我把 PID 后面的 IN 换成 “=” 后就可以了。如果要用“IN”应该怎么写呢?
为什么会这样子 大家说说见解 谢谢了 呵呵
...全文
349 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
贝隆 2010-03-02
  • 打赏
  • 举报
回复
bcrun 2010-03-02
  • 打赏
  • 举报
回复
Playfile和Playlist这两个有的PID字段的类型定义各是啥呢
MrZg007 2010-03-02
  • 打赏
  • 举报
回复
可能是括号里的select出来的是string而你的playfile.PID的格式是int所以IN会出现类型不匹配
试试
Select Playfile.FileName from Playfile where Playfile.PID IN (convert(int, select Playlist.PID from Playlist where Playlist.Name1 = '" & TextBox1.Text & "'))
liguicd 2010-03-02
  • 打赏
  • 举报
回复
帮顶....回复内容太短了!
max_shell 2010-03-02
  • 打赏
  • 举报
回复
引用 9 楼 guyehanxinlei 的回复:
写一个存储过程代替就好了

这个。。也不是很懂 呵呵 能教教么
max_shell 2010-03-02
  • 打赏
  • 举报
回复
To:Lost_Painting 谢谢
我是MS ACCESS呵呵
鸭梨山大帝 2010-03-02
  • 打赏
  • 举报
回复
1楼,没尝试就别乱讲.
还有你贴出来的代码有很大问题,如果select出两个结果,必定报错.明显楼主的需求会出现两个PID
试试下面这句,肯定报错.
select A = (CONVERT(INT,(SELECT a= '100' union SELECT a= '110')))

楼主的是什么数据库 ACCESS/MS SQL/MYSQL/Oracle ... ...?
以下语句在MS-SQL 2000中通过. 表1的PID是 int,表2的PID是 CHAR(10),使用In是没有问题的.


IF OBJECT_ID('tempdb..#TMP') > 0 DROP TABLE #TMP

Create TABLE #TMP(ID int identity(1,1) ,PID int,FileName NCHAR(10))

INSERT INTO #TMP Values(123,N'qw')
INSERT INTO #TMP Values(456,N'rt')
INSERT INTO #TMP Values(456,N'fg')
INSERT INTO #TMP Values(123,N'aa')
--INSERT INTO #TMP Values(123,NULL)

IF OBJECT_ID('tempdb..#TMP1') > 0 DROP TABLE #TMP1
Create TABLE #TMP1(ID int identity(1,1) ,PID char(10) ,Name1 NCHAR(10))

INSERT INTO #TMP1 Values(123,N'qq')
INSERT INTO #TMP1 Values(234,N'ss')


select #TMP.FileName from #TMP
where #TMP.PID IN(
select #TMP1.PID
from #TMP1
where #TMP1.Name1 = 'qq'
)


select A = (CONVERT(INT,(SELECT a= '100' union SELECT a= '110')))
引用 1 楼 mrzg007 的回复:
可能是括号里的select出来的是string而你的playfile.PID的格式是int所以IN会出现类型不匹配
试试
Select Playfile.FileName from Playfile where Playfile.PID IN (convert(int, select Playlist.PID from Playlist where Playlist.Name1 = '" & TextBox1.Text & "'))
guyehanxinlei 2010-03-02
  • 打赏
  • 举报
回复
写一个存储过程代替就好了
max_shell 2010-03-02
  • 打赏
  • 举报
回复
No null value, these are the 2 tables:
Playfile:
PID Filename
456 qw
234 rt
234 fg
123 aa

Playlist:
PID Name1
123 qq
234 ss
  • 打赏
  • 举报
回复
alter table modify PID integer not null
这只是个informix的例子。
max_shell 2010-03-02
  • 打赏
  • 举报
回复
To of123:请问怎么设置PID类型为同一种类型?谢谢了
jabulin 2010-03-02
  • 打赏
  • 举报
回复
Playfile.PID 和 Playlist.PID类型不一致所以不可以用 in
但用“=”是sql的引擎可以自动转换类型进行匹配,所以用“=”就是可以的
of123 2010-03-02
  • 打赏
  • 举报
回复

两个表的 PID 类型设置为同一种类型了吗?



7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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