zoukankan      html  css  js  c++  java
  • sql去掉ntext类型的重复记录

      在sql查询中,我们可以通过添加distinct关键字去掉重复记录,但是当表的字段中存在ntext或image类型的字段时,我们无法添加distinct关键字来过滤重复记录,查询会报错。这里有两种解决方案,一是在查询的时候将ntext类型强制转换为nvarchar的类型,并将大小设置为最大,避免丢失数据,这样就可以使用distinct关键字了;二是使用子查询来过滤查询结果。

    create table Product

    (

      id int identity(1,1) primary key,

      productName nvarchar(100),

      price decimal,

      description ntext

    )

    1.将ntext类型强制转换为nvarchar

    select distinct id,productName,price,cast(description as nvarchar(4000)) as description

    from Product

    2.使用子查询过滤

    select id,productName,price,description

    from Product

    where id in

    (

      select max(id) from Product

      group by productName,price,description//除了主键不同,其他字段内容全部相同,对其他字段进行分组

    )

  • 相关阅读:
    Java 分支结构
    Java 循环结构
    Java 运算符
    Java 修饰符
    Alpha冲刺——Day 6
    Alpha冲刺——Day 5
    Alpha冲刺——Day 4
    Alpha冲刺——Day 3
    Alpha冲刺——Day 2
    Alpha冲刺——Day 1
  • 原文地址:https://www.cnblogs.com/hnsdwhl/p/1958013.html
Copyright © 2011-2022 走看看