具有group by表达式的SELECT子句

时间:2014-05-17 07:54:10

标签: sql oracle

表名:TEST

//TEST
A#      NAME    
------------
1       CHIRS
2       MAY
3       JOHN

//APPLIES
A#        P#
---------------------
1         HELLO
2         YES
1         HAPPY

我何时使用此查询:

SELECT DISTINCT * FROM TEST t INNER JOIN APPLIES ap
on t.A#=ap.A# WHERE count(*)>1;

我想在APPLEST中显示仅显示1条或更多条记录,其中TEST.A#= APPLEIS.A#

预期产出:

A#      NAME
-------------
1       CHIRS

2 个答案:

答案 0 :(得分:1)

您应该在代码中单独指出列名。试试这个:

SELECT t.A#, t.NAME, t.ADDRESS, t.ACOMMENT
FROM TEST t INNER JOIN APPLIES ap
on t.A#=ap.A# GROUP BY t.A#, t.NAME, t.ADDRESS, t.ACOMMENT;

答案 1 :(得分:0)

按所有字段分组与使用distinct基本相同:

SELECT     DISTINCT * 
FROM       test t 
INNER JOIN applies ap ON t.A#=ap.A#;