select distinct a from tbl =
select a from tbl group by a他们的功能基本上是不一样的。
distinct消除重复行。
group by是分组语句。
GROUP BY Examples
To return the minimum and maximum salaries for each department in the employees table, issue the following statement: SELECT department_id, MIN(salary), MAX (salary)
FROM employees
GROUP BY department_id;
select a from tbl group by a他们的功能基本上是不一样的。
distinct消除重复行。
group by是分组语句。
GROUP BY Examples
To return the minimum and maximum salaries for each department in the employees table, issue the following statement: SELECT department_id, MIN(salary), MAX (salary)
FROM employees
GROUP BY department_id;
解决方案 »
- jdbc:oracle:oci8:ORCLDB的问题
- pl/sql
- 急:误删表空间如何恢复?
- 一个简单的oracle分析函数问题,请高手指点一下
- 快郁闷死我的:存储过程有输入参数和输出参数的,要在多个表中查询即:如果在表A中没有就到表B中查找,如果两个表都没有查到返回一个值
- 请教一条SQL语句
- 急问:我在linux下用pro*c远程访问数据库,应该怎么设置啊?
- oracle新手,要写procedure,有没有好的资料可以参考?有没有临时表返回数据集得例子,在线给分
- 如何存取LONG RAW字段?谢谢
- 关于oracle数据库的查询性能优化
- 讨论:调用函数可以提高效率?真的假的?
- 急救,表无法通过表空间的扩展
A表
id num
a 1
b 2
c 3
a 4
c 7
d 3
e 5如果只选出id列,用distinct和group by 一样的。
select distinct(id) from A;
id
a
b
c
d
e;
select id from A group by id;
id
a
b
c
d
e;
不同之处可能在于group by有排序功能。
但是如果需要加上另一列num,结果不同。
group by 是分组语句,如果用
select id,num from A group by id,num;
这样的结果在本例中与不加group by是一样的,因为num各个不同。
但是如果
select id,num from A group by id;
注意该语句是错误语句,因为num没有使用聚组函数,例如:sum(求和),avg(求平均数)
select id,sum(num) from A group by id;
id sum(num)
a 5
b 2
c 10
d 3
e 5用distinct不显示重复的行。
在本例中
select distinct id,num from A;的结果也和不加distinct一致。
因为id,num没有重复的行,而不是只看id。group by 功能更强大一些,另外推荐使用group by。
因为distinct会导致全表扫描,而group by如果索引建的
恰当的话,会有性能上的提高。
DISTINCT一下人类,可以发现分为:男人,女人;
GROUP BY一下人类,可以知道男人和女人的情况(人数,最大,最小)。