急,求一个SQL语句!!!!

ghlfllz 2004-12-22 05:54:56
有一个表,表的部分结构如下
custom
custom_name product_name from_port
varchar(50) varchar(100) varchar(20)

我想使用一个SQL从上表面的表中选择这三列
在结果中要求custom_name、Product_name,from_port等列中不能重复
而三个列需要同时列出来,如果那一个列的数据不够长,则用NULL表示

结果如下
custom_name product_name from_port
w w w
r r r
… … …
null w w
null w null
...全文
199 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghlfllz 2004-12-27
  • 打赏
  • 举报
回复
算了吧,看来是这个比较难

还是用自己想的那个办法凑合着用吧
ghlfllz 2004-12-25
  • 打赏
  • 举报
回复
这个语句我已经试过了,可以满足我的要求的
而且在去掉了重复数据的同时,还可以区分开来各种不同的数据

重复数据的去除在于关键字DISTINCT的使用

但我这种方法还不是很好,就是各个字段放在同一个字段里面了
没有将它们单独存放

各位有更好的办法吗
ghlfllz 2004-12-24
  • 打赏
  • 举报
回复
各位还有更好的办法吗
ghlfllz 2004-12-24
  • 打赏
  • 举报
回复
谢谢 zjcxc(邹建)
这个方法可以解决部分问题,我也想过这个方法
但是后来发现这个不行,因为我的这三个字段中的内容的长度不一样
有可能第一个字段中有4000条内容,第二个字段中有5400条内容,而第三个字段中只有2500
在数据的条数参差不齐的时候数据就不完整了

我自己也想到了一个办法,但是是把所有的字段放在同一个字段列表中了
select DISTINCT import_customid,'1' as operate from customimport
union all
select DISTINCT import_product,'2' as operate from customimport
long111 2004-12-24
  • 打赏
  • 举报
回复
楼主,如果你用union all的话,那么重复的数据还是没有消除
zjcxc 元老 2004-12-23
  • 打赏
  • 举报
回复
select id=identity(int),custom_name
into #t1 from(select distinct custom_name from custom)a

select id=identity(int),product_name
into #t2 from(select distinct product_name from custom)a

select id=identity(int),from_port
into #t3 from(select distinct from_port from custom)a

select custom_name,product_name,from_port
from #t1 a
full join #t2 b on a.id=b.id
full join #t3 c on a.id=c.id
ghlfllz 2004-12-23
  • 打赏
  • 举报
回复
custom表的部分结构如下
custom_name product_name from_port
varchar(50) varchar(100) varchar(20)

custom表中有大约100W条记录,然后在这些记录中
custom_name、Product_name、from_port等三个字段中分别有很多重复的值,我想使用一个SQL从上表面的表中选择这三列,并需要确保custom_name、Product_name,from_port等列中的值不能重复

这样一来如果custom_name中有450个互不重复的值,而product_name和from_port中只有320个值
那么在最后页的130条中product_name和from_port的值都为null

结果如下
custom_name product_name from_port
w w w
r r r
… … …
null e h
null r null

不知道这样子说我的需求说清楚了没有
quanyi 2004-12-22
  • 打赏
  • 举报
回复
其實没有看懂你的需求!
quanyi 2004-12-22
  • 打赏
  • 举报
回复
select distinct custom_name , product_name , from_port from custom
quanyi 2004-12-22
  • 打赏
  • 举报
回复
select custom_name product_name from_port from custom
xx_adam 2004-12-22
  • 打赏
  • 举报
回复
好像没有看懂你的需求!
NinGoo 2004-12-22
  • 打赏
  • 举报
回复
看晕了,custom里面原来的数据是什么样的?

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧