下面的代码工作正常,有一个问题,显示许多记录,它假设只显示一个。 (例如:如果来自 UserInfo 表的用户在广告表格中有10个广告,其中5个广告的值类似于查询字符串结果值,所以他重复用户5次,无论他有什么广告 Wtag 列,它都会出现。)
if (Request.QueryString["Searchfor"] != null)
{
using (SqlConnection srcbizhsql = new SqlConnection(sc))
{
srcbizhsql.Open();
SqlDataAdapter DAsearchbiz = new SqlDataAdapter(@"SELECT DISTINCT UI.[UID],UI.[Country],UI.[State],UI.[City],UI.[Logo],UI.[Website],UI.[UsrType],UI.[BizCateg],UI.[BizSubCateg],UI.[Twitter],UI.[GooglePlus],UI.[Facebook],UI.[CompNme],UI.[RegDate] ,SUBSTRING([CompDesc],1,40) AS CompDesc ,AD.[Wtags] FROM UserInfo AS UI , ads AS AD WHERE UI.UID = AD.UID and UI.[Country]= @Location AND UI.[UsrType]= 'Business' AND CHARINDEX(@Wtag, AD.[Wtags] )>0 ORDER BY UI.[RegDate] DESC", sc);
DataSet DSsrchBiz = new DataSet();
DAsearchbiz.SelectCommand.Parameters.AddWithValue("@Location", cookie.Value);
DAsearchbiz.SelectCommand.Parameters.AddWithValue("@Wtag", result);
DAsearchbiz.Fill(DSsrchBiz);
SrchbizHomLstviw.DataSource = DSsrchBiz.Tables[0];
SrchbizHomLstviw.DataBind();
SrchMultiView.ActiveViewIndex = 1;
RebindAds();
RebindJobs();
}
}
答案 0 :(得分:0)
你真的应该开始使用ANSI-92样式连接而不是旧的ANSI-89样式。语法已经有20多年了。 Bad habits to kick : using old-style JOINs
以下是“现代”联接和一些格式的查询。
SELECT DISTINCT UI.[UID]
, UI.[Country]
, UI.[State]
, UI.[City]
, UI.[Logo]
, UI.[Website]
, UI.[UsrType]
, UI.[BizCateg]
, UI.[BizSubCateg]
, UI.[Twitter]
, UI.[GooglePlus]
, UI.[Facebook]
, UI.[CompNme]
, UI.[RegDate]
, SUBSTRING([CompDesc], 1, 40) AS CompDesc
, AD.[Wtags]
FROM UserInfo AS UI
JOIN ads AS AD ON UI.UID = AD.UID
WHERE UI.[Country] = @Location
AND UI.[UsrType] = 'Business'
AND CHARINDEX(@Wtag, AD.[Wtags])> 0
ORDER BY UI.[RegDate] DESC
然而,我不明白的是问题所在。
答案 1 :(得分:0)
感谢所有人在经过多次尝试后修复了
SELECT DISTINCT UI.[UID] ,UI.[Country] ,UI.[State] ,UI.[City] ,UI.[Logo] ,UI.[Website] ,UI.[UsrType] ,SUBSTRING(UI.[CompDesc], 1, 60) AS CompDesc ,UI.[BizCateg] ,UI.[BizSubCateg] ,UI.[Twitter] ,UI.[GooglePlus] ,UI.[Facebook] ,UI.[CompNme] ,UI.[RegDate] FROM UserInfo AS UI JOIN (SELECT DISTINCT ads.[UID] FROM ads WHERE CHARINDEX(@Wtag, ads.[Wtags])> 0) AS AD ON AD.[UID] = UI.[UID] WHERE UI.[Country] = @Location AND UI.[UsrType] = 'Business' ORDER BY UI.[RegDate] DESC