在具有except的sql语句中使用order by

时间:2014-10-21 05:10:21

标签: sql

我正在尝试按字母顺序从我的查询中命名全名,但我不知道在哪里放置order by语句。

这是声明

SELECT DISTINCT 
    fullname 
FROM 
    Account 
WHERE
    account_id != ? 
ORDER BY
    firstName 

EXCEPT 

SELECT DISTINCT 
   name_of_friend 
FROM 
   Friend 
WHERE
   account_id = ?

2 个答案:

答案 0 :(得分:1)

我认为您需要在代码中更改以下内容

SELECT DISTINCT 
    fullname 
FROM 
    Account 
WHERE
    account_id != ?  

EXCEPT     

SELECT 
   name_of_friend 
FROM 
   Friend 
WHERE
   account_id = ?
ORDER BY
    firstName

答案 1 :(得分:0)

在sqlserver数据库中你可以这样做..(抱歉没有50分添加评论所以添加答案)

SELECT DISTINCT fullname 
FROM Account 
WHERE account_id != ? 
AND NOT EXISTS (SELECT NULL FROM FRIENDS WHERE ACCOUNT_ID = ?)
ORDER BY firstName