DISTINCT结果为ORA-01791:不是SELECTed表达式

时间:2011-03-31 22:21:44

标签: oracle10g

select DISTINCT a.FNAME||' '||a.LNAME
   from AUTHOR a, books B, BOOKAUTHOR ba, customers C, orders
   where C.firstname='BECCA'
      and C.lastname='NELSON'
      and a.AUTHORID=ba.AUTHORID
      and b.ISBN=bA.ISBN
   order by a.LNAME

给出ORA-01791:不是SELECTed表达式 但没有DISTINCT。

如何让它发挥作用?

1 个答案:

答案 0 :(得分:25)

只需在select子句中将LNAME作为列添加:

SELECT full_name
FROM (
 select DISTINCT a.FNAME||' '||a.LNAME AS full_name, a.LNAME
 from AUTHOR a, books B, BOOKAUTHOR ba, customers C, orders
 where C.firstname='BECCA'
   and C.lastname='NELSON'
   and a.AUTHORID=ba.AUTHORID
   and b.ISBN=bA.ISBN
 )
order by a.LNAME

如果您只想要输出中的第一列,则可以将整个事物放在子查询中。