ITeye博客

出道distinct相关的sql题给大家做做

coderplay · 2009-05-22 20:13 · misc · 数据库 · Markdown

这几天在做sql编译相关的东西, 自己弄了个题目,连资深数据库开发人员都可能会搞错. 以下sql中哪些执行时会报错? (适于所有常见DBMS)
1.

select
   distinct col
from
  tbl

2.

select
  distinct col1,
  distinct col2
from
  tbl  

3.

select
   count(distinct col% 10)
from
  tbl

 4.

select
   length(distinct col1% 10)
from
  tbl

 5.

select
   sum(distinct col1% 10),  
   sum(distinct col1% 9)
from
  tbl

 6.

select
  distinct col1,
  count(distinct col2)
from
  tbl

 

 7.

select
  sum(distinct col1 % 10),
  count(distinct col2 % 9)
from
  tbl

 8.

select
  max(distinct substr(col1, 1, 10)),
  count(distinct col2 % 9)
from
  tbl

评论(3)

  1. #3 楼 · coderplay · 2009-07-06 12:00

    @xieye 此题和group by 无关, 使不使用group by那得根据查询需求. @Ahu_425 2,4,6出错. disinct 和 aggregation(distinct )是两回事, 虽然是同一关键字. 函数接disinct只能是aggregation函数, length是普通函数,所以不行的.

  2. #2 楼 · xieye · 2009-07-01 14:00

    建议多用group by

  3. #1 楼 · Ahu_425 · 2009-07-01 11:18

    LZ的题目很有意思 第2条和第4条会容易出错吧 第2条是因为两个distinct单独同时用时会有冲突 第4条是因为类型不匹配吧 还望指教

评论功能维护中,当前仅展示历史评论。