如何更新/编辑SQL代​​码以使其可行

时间:2014-12-01 10:19:03

标签: sql oracle

我是SQL的新手,我需要更新一段我需要用于工作的SQL代码,但我不知道如何将它变成一段可行的代码。我已经尝试在Oracle SQL Developer中编辑代码,但我所做的任何编辑根本不喜欢。

有人可以帮助我使下面的代码可行。该代码应该获取在数据库中具有相同名称的任何用户,以便我可以看到任何重复项,然后按字母顺序对它们进行排序。代码是很久以前写的,我在编辑它时遇到了麻烦,所以我可以在Oracle SQL Developer中使用。

SELECT surname, forename, login_id, site, position "
        + "FROM users "
        + "WHERE (surname, forename) IN "
        +    "( SELECT surname, forename "
        +      "FROM users "
        +      "GROUP BY surname, forename "
        +      "HAVING COUNT(*)>1 ) "
        + "ORDER BY 1 ASC, 2 ASC ";

我尝试了以下但无济于事。

SELECT surname,
  forename,
login_id,
site_id,
position
FROM users
WHERE surname
forename
FROM users
GROUP BY surname,
forename
HAVING COUNT (*)>1
ORDER BY 1 ASC,
2 ASC;

谢谢

2 个答案:

答案 0 :(得分:2)

试试这个(没有任何引号或+

SELECT surname, forename, login_id, site, position 
FROM users 
WHERE (surname, forename) IN 
        ( SELECT surname, forename 
        FROM users 
        GROUP BY surname, forename 
        HAVING COUNT(*)>1 ) 
ORDER BY 1 ASC, 2 ASC;

答案 1 :(得分:0)

SELECT surname, forename, login_id, site, position 
FROM users 
WHERE (surname||forename) IN 
        ( SELECT surname||forename 
        FROM users 
        GROUP BY surname, forename 
        HAVING COUNT(*)>1 ) 
ORDER BY 1,2;