• SQL语句の集锦


    7.某一个字段重复根据另一个字段取最新的一条

    例如:progress_id 是主键 int 自增

    select * from table_XXX


    select progress_id,detailed_ListId progress_detailed_ListId,workamount,createuser progress_Createuser ,createtime progress_Createtime,(select sum(workamount)sumworkamount from  table_XXX)sumworkamount from table_XXX where progress_id in(
    select max(progress_id) from table_XXX group by detailed_listid
    )

    6.删除数据后根据主键从备份表中恢复

    insert  sameTable_1 (name,dz) select name,dz from sameTable_1_bak where not exists (select 1  from sameTable_1 where sameTable_1_bak.itmid=sameTable_1.itmid)

    5.查询数据库所有表的大小

    exec sp_MSforeachtable 'exec sp_spaceused ''?'''  

    4.根据某一个字段重复只取第一条数据

    select * from  News_ITM a left join
    (select * from (
     select *, row_number() over (partition by newid order by msg desc) as group_idx  
        from NewsComment_ITM) s where group_idx = 1) b on
        a.id = b.newId

    3.即时获取新增自增ID值

    string sql="insert into XX表 values(aa,bb,cc);select @@IDENTITY";

    2.时间为NULL,转成空字符串

    select  ISNULL(convert(varchar(100),subDateTime,20),'') ,subDateTime from News_ITM

    1.已有字段添加主键(修改)

    alter table [dbo].[News_ITM] add Constraint primarykey  primary key(id)

  • 相关阅读:
    PostgreSQL 10编译安装(CentOS 7)
    CentOS安装单机Zookeeper
    [Oracle报错]TNS-12535: TNS:operation timed out、TNS-00505: Operation timed out
    hibernate一级缓存及对象的状态
    hibernate框架的简单入门
    Json和Ajax
    sql多行多列重复
    折线图饼状图柱形图
    XML文件的读取
    Json数据产生树形结构
  • 原文地址:https://www.cnblogs.com/dabexiong/p/4978338.html
Copyright © 2020-2023  润新知