日期:2014-05-17  浏览次数:20433 次

帮我看条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,1
union 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

-->try
string SqlStr2 = "select username,touxiang from usermessage where username=(select username from table20 where id='" + id2 + "')";