日期:2014-05-19  浏览次数:20486 次

请高手帮我解决:写一个"相关新闻/文章"的SQL语句,高手一定进来看
Select   ID,Title,Type,KeyWords,TextSource,CreateDate,Content   From   TC_News   Where   KeyWords   =   Select   KeyWords   From   TC_News   Where   ID=2

这是我写的一个SQL语句,但定个SQL语句语法一定是错误的,请高手帮忙解决.
我要实现的功能就是在网站页面上根据关键字(字段是KeyWords)显示某篇文章(比如:ID=2的文章)的相关文章标题(字段是Title).
请高手帮忙解决!谢谢!

------解决方案--------------------
Select ID,Title,Type,KeyWords,TextSource,CreateDate,Content From TC_News Where KeyWords = (Select KeyWords From TC_News Where ID=2) T
------解决方案--------------------
Select ID,Title,Type,KeyWords,TextSource,CreateDate,Content From TC_News Where KeyWords in (Select KeyWords From TC_News Where ID=2)
------解决方案--------------------
SqlDataAdapter sda = new SqlDataAdapter( "Select ID,Title,Type,KeyWords,TextSource,CreateDate,Content From TC_News Where KeyWords in (Select KeyWords From TC_News Where ID= "+int.parse(Request.Params[ "ID "])+ ") ", conn);

------解决方案--------------------
SqlDataAdapter sda = new SqlDataAdapter( string.Format( "Select
ID,Title,Type,KeyWords,TextSource,CreateDate,Content From TC_News Where ID <> {}
and KeyWords in (Select KeyWords From TC_News Where ID={0}) ",
Request.Params[ "ID "]), conn);

sql server编译时会将“in”翻译为inner join然后才开始进行优化。如果你懂T-SQL,最好直接写inner join的代码更容易人工看出优化的可能性。