您的位置:首页 > 数据库

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"] += "&nbsp";
                            }
                           
                            //判断是否为空
                            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

  • 点赞
  • 收藏
  • 分享
  • 文章举报
daybreak0805 发布了0 篇原创文章 · 获赞 0 · 访问量 149 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: