sql 把两张表组合成一张表的实例
2020-02-02 20:12
501 查看
public DataTable ITbSeeArti(string id)
{
string idstr = "";
EBM.DBUtility.DbHelperSQL.ExecuteSql("update tbArticle set arVisitCounts=arVisitCounts+1 where arid=" + id);
string sql = "select arid,artitle,arTitleEn,arUpdatetime,arColumn,arPdfDownCounts,arVisitCounts,arNumber,arPageCount,arPdfUrl,arKeywords,arKeywordsEn,arClassificationNum,arDol,arDocumentMark,arAbstract,arAbstractEn,arMemo,arReferences from tbarticle where arid=" + id;
DataSet ds = EBM.DBUtility.DbHelperSQL.Query(sql);
if (ds.Tables[0].Rows.Count > 0)
{
ds.Tables[0].Columns.Add("author");
ds.Tables[0].Columns.Add("LikeAr");
ds.Tables[0].Columns.Add("work");
foreach (DataRow dr in ds.Tables[0].Rows)
{
//添加作者姓名
DataSet auth = EBM.DBUtility.DbHelperSQL.Query(string.Format("select auid,auname,ausubtitle,auworkunit from tbauthor where auarticleid={0}", dr["arid"]));
if (auth.Tables[0].Rows.Count > 0)
{
int i = 0;
foreach (DataRow _dr in auth.Tables[0].Rows)
{
if (i > 0)
{
dr["author"] += "|";
idstr += ",";
//
}
//dr["work"]+=string.Format("{0}",_dr["auworkunit"]) ;
dr["author"] += string.Format("{0},{1},{2}", _dr["auid"], _dr["auName"], _dr["ausubtitle"]);
idstr += _dr["auid"];
i++;
}
}
else
{
dr["author"]="";
idstr = "0";
//dr["work"] = "";
}
//添加作者工作地点
DataSet dswork = EBM.DBUtility.DbHelperSQL.Query("select distinct auworkunit from tbauthor where auid in(" + idstr + ")");
if (dswork.Tables[0].Rows.Count > 0)
{
int i = 0;
foreach (DataRow drw in dswork.Tables[0].Rows)
{
if (i > 0)
{
dr["work"] += " ";
}
//判断是否为空
dr["work"] +=string.Format("{0}",drw["auworkunit"]) ;
i++;
}
}
else
{
dr["work"] ="";
}
//添加相似文章
DataSet likeAr = EBM.DBUtility.DbHelperSQL.Query(string.Format("select arid,artitle from tbarticle where (arkeywords like '%{0}%' or arkeywordsen like '%{1}%') and arid!={2}", dr["arKeywords"], dr["arkeywordsen"],dr["arid"]));
if (likeAr.Tables[0].Rows.Count > 0)
{
int j = 0;
foreach (DataRow dr_ in likeAr.Tables[0].Rows)
{
if (j > 0)
dr["LikeAr"] += "|";
dr["LikeAr"] += string.Format("{0},{1}", dr_["arid"], dr_["artitle"]);
j++;
}
}
else
{
dr["LikeAr"] ="";
}
}
}
return ds.Tables[0];
}
{
string idstr = "";
EBM.DBUtility.DbHelperSQL.ExecuteSql("update tbArticle set arVisitCounts=arVisitCounts+1 where arid=" + id);
string sql = "select arid,artitle,arTitleEn,arUpdatetime,arColumn,arPdfDownCounts,arVisitCounts,arNumber,arPageCount,arPdfUrl,arKeywords,arKeywordsEn,arClassificationNum,arDol,arDocumentMark,arAbstract,arAbstractEn,arMemo,arReferences from tbarticle where arid=" + id;
DataSet ds = EBM.DBUtility.DbHelperSQL.Query(sql);
if (ds.Tables[0].Rows.Count > 0)
{
ds.Tables[0].Columns.Add("author");
ds.Tables[0].Columns.Add("LikeAr");
ds.Tables[0].Columns.Add("work");
foreach (DataRow dr in ds.Tables[0].Rows)
{
//添加作者姓名
DataSet auth = EBM.DBUtility.DbHelperSQL.Query(string.Format("select auid,auname,ausubtitle,auworkunit from tbauthor where auarticleid={0}", dr["arid"]));
if (auth.Tables[0].Rows.Count > 0)
{
int i = 0;
foreach (DataRow _dr in auth.Tables[0].Rows)
{
if (i > 0)
{
dr["author"] += "|";
idstr += ",";
//
}
//dr["work"]+=string.Format("{0}",_dr["auworkunit"]) ;
dr["author"] += string.Format("{0},{1},{2}", _dr["auid"], _dr["auName"], _dr["ausubtitle"]);
idstr += _dr["auid"];
i++;
}
}
else
{
dr["author"]="";
idstr = "0";
//dr["work"] = "";
}
//添加作者工作地点
DataSet dswork = EBM.DBUtility.DbHelperSQL.Query("select distinct auworkunit from tbauthor where auid in(" + idstr + ")");
if (dswork.Tables[0].Rows.Count > 0)
{
int i = 0;
foreach (DataRow drw in dswork.Tables[0].Rows)
{
if (i > 0)
{
dr["work"] += " ";
}
//判断是否为空
dr["work"] +=string.Format("{0}",drw["auworkunit"]) ;
i++;
}
}
else
{
dr["work"] ="";
}
//添加相似文章
DataSet likeAr = EBM.DBUtility.DbHelperSQL.Query(string.Format("select arid,artitle from tbarticle where (arkeywords like '%{0}%' or arkeywordsen like '%{1}%') and arid!={2}", dr["arKeywords"], dr["arkeywordsen"],dr["arid"]));
if (likeAr.Tables[0].Rows.Count > 0)
{
int j = 0;
foreach (DataRow dr_ in likeAr.Tables[0].Rows)
{
if (j > 0)
dr["LikeAr"] += "|";
dr["LikeAr"] += string.Format("{0},{1}", dr_["arid"], dr_["artitle"]);
j++;
}
}
else
{
dr["LikeAr"] ="";
}
}
}
return ds.Tables[0];
}
转载于:https://www.cnblogs.com/sunnysmile/archive/2009/06/10/1500409.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 动态组合SQL语句方式实现批量更新的实例
- [SQL]组合两张表
- 动态组合SQL语句方式实现批量更新的实例
- 如何用Sql把两张字段不相同的表合成一张表
- SQL编程实例:Access数据库,两张表的统计,count、sum聚合函数的使用,iif的使用,group by的使用
- SQL 把一张表虚拟成两张表
- Net设计模式实例之组合模式(Composite Pattern)
- SQL存储过程实例 (有点乱!!)
- 数据库一张表的数据拷贝到另一张表中SQL语句
- python组合无重复三位数的实例
- sql 实例 搜集
- SQL语句实现表中字段的组合累加排序
- SQL语句实例
- SQL server中如何只备份一张数据库表的数据
- provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错
- SQL语句练习实例之七 剔除不需要的记录行
- db2数据库创建一张表,并为该表加上主键递增的性能和中间表的创建的sql语句
- mysql 如何用一条SQL将一张表里的数据插入到另一张表 3个例子
- Sql 常用的语句实例与代码
- Jboss配置 MS SQL SERVER 2008数据库连接池实例