SQL删除指定字段问题

论坛 期权论坛     
选择匿名的用户   2021-5-23 02:05   0   0
<p>更新下问题: <br>表:TBL_Info <br>字段: <br>    infoId int <br>    title  varchar(20) <br>    Content  text <br>    byUser  varchar(20) <br>    createTime datetime <br><br>1、如何删除表中数据相同的数据呢?(主键除外) <br>2、如何删除数据表中某个字段数据相同的数据呢(比如title:删除所有title相同的数据)? <br>3、如何统计表中title相同数据的数目?</p>
<p> </p>
<p><span style="color: #008080;">--</span><span style="color: #008080;">1</span><span style="color: #008080;"> --</span><span style="color: #008080;">1.1相同时保留最小的infoId</span><span style="color: #008080;"><br></span><span style="color: #0000ff;">delete</span><span style="color: #000000;"> TBL_Info </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> TBL_Info t </span><span style="color: #0000ff;">where</span><span style="color: #000000;"> infoId </span><span style="color: #808080;">not</span><span style="color: #000000;"> </span><span style="color: #808080;">in</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">select</span><span style="color: #000000;"> </span><span style="color: #ff00ff;">min</span><span style="color: #000000;">(id) </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> infoId </span><span style="color: #0000ff;">where</span><span style="color: #000000;"> title </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.title </span><span style="color: #808080;">and</span><span style="color: #000000;"> Content </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.Content </span><span style="color: #808080;">and</span><span style="color: #000000;"> byUser </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.byUser </span><span style="color: #808080;">and</span><span style="color: #000000;"> createTime </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.createTime)<br></span><span style="color: #008080;">--</span><span style="color: #008080;">1.1相同时保留最大的infoId</span><span style="color: #008080;"><br></span><span style="color: #0000ff;">delete</span><span style="color: #000000;"> TBL_Info </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> TBL_Info t </span><span style="color: #0000ff;">where</span><span style="color: #000000;"> infoId </span><span style="color: #808080;">not</span><span style="color: #000000;"> </span><span style="color: #808080;">in</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">select</span><span style="color: #000000;"> </span><span style="color: #ff00ff;">max</span><span style="color: #000000;">(id) </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> infoId </span><span style="color: #0000ff;">where</span><span style="color: #000000;"> title </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.title </span><span style="color: #808080;">and</span><span style="color: #000000;"> Content </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.Content </span><span style="color: #808080;">and</span><span style="color: #000000;"> byUser </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.byUser </span><span style="color: #808080;">and</span><span style="color: #000000;"> createTime </span><span style="color: #808080;">&#61;</span><span style="color: #000000;"> t.createTime)<br><br></span><span style="color: #008080;">--</span><span style="color: #008080;">2</span><span style="color: #008080;"><br></span><span style="color: #0000ff;">delete</span><span style="color: #000000;"> </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> TBL_Info </span><span style="color: #0000ff;">where</span><span style="color: #000000;"> title </span><span style="color: #808080;">in</span><span style="color: #000000;"> (</span><span style="color: #0000ff;">select</span><span style="color: #000000;"> title </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> TBL_Info </span><span style="color: #0000ff;">group</span><span style="color: #000000;"> </span><span style="color: #0000ff;">by</span><span style="color: #000000;"> title </span><span style="color: #0000ff;">having</span><span style="color: #000000;"> </span><span style="color: #ff00ff;">count</span><span style="color: #000000;">(</span><span style="font-weight: bold; color: #800000;">1</span><span style="color: #000000;">) </span><span style="color: #808080;">&gt;</span><span style="color: #000000;"> </span><span style="font-weight: bold; color: #800000;">1</span><span style="color: #000000;">)<br><br></span><span style="color: #008080;">--</span><span style="color: #008080;">3</span><span style="color: #008080;"><br></span><span style="color: #0000ff;">select</span><span style="color: #000000;"> title , </span><span style="color: #ff00ff;">count</span><span style="color: #000000;">(</span><span style="font-weight: bold; color: #800000;">1</span><span style="color: #000000;">) </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> TBL_Info </span><span style="color: #0000ff;">group</span><span style="color: #000000;"> </span><span style="color: #0000ff;">by</span><span style="color: #000000;"> title<br></span><span style="color: #0000ff;"
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP