首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

帮小弟我看条SQL语句

2012-09-24 
帮我看条SQL语句select username,touxiang from usermessage where username(select username from table

帮我看条SQL语句
select username,touxiang from usermessage where username=(select username from table20 where id=1)
这一条,查询没有任何值
可是,把这句分开成两句,查询就可以获得值,如下:
select username from table20 where id=1 这条得值username=1

然后,
select username,touxiang from usermessage where username=1
这条也有值,得username=1,touxiang=<img url="~/image/usertouxiang/1.jpg" /

既然分开查询,能得到值,为什么连接起来就查询不到值呢? 求助。

[解决办法]

SQL code
--按你的描述是有值的,沒值的原因可能是username是字符型的,左邊可能有空格或字段中包含其他字符,如tab,這種是看不出來的--你select len(username)看一下with table20 (id,username) as(select 1,1union all select 1,2),usermessage (username,touxiang ) as(select 1,'x')select username,touxiang from usermessage where username=(select username from table20 where id=1)/*username    touxiang----------- --------1           x(1 row(s) affected)*/
[解决办法]
C# code
-->trystring SqlStr2 = "select username,touxiang from usermessage where username=(select username from table20 where id='" + id2 + "')"; 

热点排行
Bad Request.