精妙的SQL语句收藏

  • 说明:复制表(只复制结构,源表名:a 新表名:b)
    select * into b from a where 1<>1
  • 说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
    insert into b(a, b, c) select d,e,f from b;
  • 说明:日程安排提前五分钟提醒
    select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
  • 说明:两张关联表,删除主表中已经在副表中没有的信息
    delete from info where not exists ( select * from infobz where info.infid=infobz.infid )
  • 说明:清除表中重复的记录(PriKey为递增值主键,key为判断相同记录的标准)
    Delete From B Where B.PriKey In(Select B.PriKey from B As s1 Where B.PriKey not in (Select max(B.PriKey) from B As s2 Where s1.key=s2.key))
原文地址:https://www.cnblogs.com/tangself/p/1621427.html