SQL-Select在组合时以错误结束

时间:2015-11-02 11:29:34

标签: sql sqlbase sqltalk

我尝试对一些重复项进行简单的选择,但我的sql语句总是以这个错误结束:

  

命令未正确结束

我拥有的是这样一张桌子

EAN              STR
=============    ====
8030524096397    AAAA
8030524096397    BBBB
8030524096731    XXXX
8030524096731    YYYY
8030524096324    CCCC

我的选择实际上很简单

SELECT EAN, COUNT(*) FROM ean GROUP BY ean HAVING COUNT(*) > 1;

Reults:

EAN               COUNT(*)
=============     ========
8030524096397        2
8030524096731        2

直到这里一切都很好!现在我想要重复的STR并试试这个

SELECT * FROM EAN E 
    INNER JOIN ( SELECT EAN, COUNT(*) FROM ean GROUP BY ean HAVING COUNT(*) > 1 )  
R ON 
E.EAN = R.EAN;

但这会导致此错误。

确切地说:

SELECT * FROM EAN E
INNER JOIN ( SELECT EAN, COUNT(*) FROM ean GROUP BY ean HAVING COUNT(*) > 1 )  R ON
^
Error: Command not properly ended

我做错了什么?

DB的信息: Gupta Centura SQLBase 7.0.1

3 个答案:

答案 0 :(得分:3)

我不认为SQLBase 7.01支持正确的ANSI JOIN语法(除了:使用更现代产品的一个很好的理由)。该错误表示INNER JOIN上存在问题。

以下是两种可能的解决方案。

首先,令人讨厌的古怪join语法:

SELECT *
FROM EAN E,
     ( SELECT EAN, COUNT(*) as cnt FROM ean GROUP BY ean HAVING COUNT(*) > 1 )  
R 
WHERE E.EAN = R.EAN;

其次,IN

SELECT *
FROM EAN E
WHERE E.EAN IN ( SELECT EAN FROM ean GROUP BY ean HAVING COUNT(*) > 1 )  

答案 1 :(得分:0)

SELECT * 
FROM EAN E 
INNER JOIN 
    (SELECT EA.EAN, COUNT(*) Cnt 
    FROM ean EA
    GROUP BY EA.ean 
    HAVING COUNT(*) > 1 
    ) R ON E.EAN = R.EAN;

我将别名Cnt给了COUNT(*)。这应该会使错误消失。

答案 2 :(得分:0)

尝试此操作(使用AS关键字向表中添加别名)

SELECT * FROM EAN AS E 
INNER JOIN 
(SELECT EAN, COUNT(*) FROM ean GROUP BY ean HAVING COUNT(*) > 1) AS R 
ON 
E.EAN = R.EAN;