sql语句

SQL查询与更新案例
本文提供了一系列SQL查询实例,包括从不同表中联合查询特定条件的数据,并展示了如何进行SQL更新操作。此外,还介绍了使用C#连接数据库并执行SQL命令的方法。

 1.select top 12 * from JackDonkey_Album where AlbumId in(select AlbumId from JackDonkey_TagImgThou whereCONVERT(varchar(50), TagId) ='489') and RowStatus='active' order by YesCount DESC

2.((select * from (select top 13 'Picture' as ImgCat, a.ImgNameas Name,a.ImgId as ImgId,
 a.ImgUrl as ImgUrl, b.UserName as Username,a.CreatedDate as CreatedDate, b.DonkeyShowCountas DSC from JackDonkey_Picture
  a,JackDonkey_User b
where a.ImgId in (select ImgId from JackDonkey_TagImgThou where TagId=45)
  and a.rowstatus='active' and b.userid=a.UserId order by a.CreatedDate desc)abc)
  Union all(select * from (select top 13  'Album' as ImgCat, a.AlbumName as Name,a.AlbumId as ImgId,a.BigImgUrl
  as ImgUrl,b.UserName as Username,a.CreatedDate as CreatedDate,b.DonkeyShowCount as DSC from JackDonkey_Album a,
  JackDonkey_User b where a.AlbumId in (select AlbumId from JackDonkey_TagImgThou where TagId=45) and a.rowstatus='active'
   and b.userid=a.UserId order by a.CreatedDate desc) abc) Union all(select * from (select top 13 'Profile' as ImgCat,
   Username as Name, Userid as ImgId, AvatarImg as ImgUrl, UserName as USername,CreatedDate as CreatedDate,DonkeyShowCount
   as DSC from JackDonkey_User where UserId in (select UserId from JackDonkey_UserThought where  Id in (select ThouId
   from JackDonkey_TagImgThou where TagId=45)) and UserType=1 and isactive =1 and rowstatus='active'
   order by CreatedDate desc) abc)) order by CreatedDate DESC
  

2".(SELECT  a.*,b.userid,'P' as imageTypeFROM JackDonkey_PictureComment a
inner join JackDonkey_Picture b on a.PictureId=b.ImgId
 Where b.userid=24
 
 union all
 
SELECT a.id as id ,a.AlbumId as PictureId,a.userid as UserId, a.UserName as UserName,
 a.Comment as Comment ,a.RowStatus as RowStatus,
 a.CreatedDate as CreatedDate, a.YesCount as YesCount,a.NoCount as NoCount ,b.userid as userid ,'A' as imageType
 FROM JackDonkey_AlbumComment a inner join JackDonkey_Album b on a.AlbumId=b.AlbumId 
 Where b.userid=24 and a.RowStatus='active'
 
 union all
 
SELECT a.id as id ,a.ThoughtId as PictureId,a.UserId as UserId, a.UserName as UserName,
a.Comment as Comment ,a.RowStatus as RowStatus, a.CreatedDate as CreatedDate, a.YesCount as YesCount,
a.NoCount as NoCount ,b.userid as userid ,'T' as imageType
FROM JackDonkey_UserThoughtComment a inner join JackDonkey_UserThought b on a.ThoughtId=b.Id 
Where b.userid=24 and a.RowStatus='active'
 
 )
 order by a.createddate desc


3. SQL 更新操作:update JackDonkey_User set Tag1=‘11’,Tag2=‘11’,Tag3=‘1’ where UserId=1

4.JTD:   string str = txtTags.Text.Trim();
        char strsplit = ',';
        string[] tags = str.Split(strsplit);
          // savs to the SQL
         string strconn = System.Configuration.ConfigurationManager.AppSettings["Main.ConnectionString"].Trim() + " ; Provider=SQLOLEDB;";
        OleDbConnection cnn = new OleDbConnection(strconn);
        string sql = "select *  from JackDonkey_Tag  where TagId in (select TagId from  JackDonkey_TagImgThou where ImgId=" + lblPicId.Text + " ) order by TagId";
        try
        {
            cnn.Open();
            OleDbCommand cmd = new OleDbCommand(sql, cnn);
            OleDbDataReader read = cmd.ExecuteReader();
            List<int> TagId = new List<int>();
            while (read.Read())
            {               
                TagId.Add(Convert.ToInt32 (read["TagId"]));
            }
            //update TagName
            for (int i = 0; i < TagId.Count;i++ )
            {
                string sql_update = "update JackDonkey_Tag set TagName='" + tags[i] + "' where TagId=" + TagId[i];
                OleDbCommand cmd_update = new OleDbCommand(sql_update, cnn);
                cmd_update.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            if (cnn.State == ConnectionState.Open)
            {
                cnn.Close();
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值