错误111,无效使用组功能

时间:2016-06-01 03:42:50

标签: mysql mysql-workbench mysql-error-1111

SELECT DISTINCT CONCAT('TOTAL NUMBER OF LEGS IN A TRIP NUMBER ', T1.TNUM, ' IS NOT EQUAL TO THE LARGEST NUMBER OF LEG IN THE TRIP') AS 'ERR'
FROM TRIPLEG T1
WHERE MAX(T1.LEGNUM) = (SELECT COUNT(T2.TNUM) 
                    FROM TRIPLEG
                    WHERE T1.TNUM = T2.TNUM);

使用我的代码,我试图找到违反以下一致性约束的所有行程。 每次旅行的最后一站必须等于旅程中的腿总数“

CREATE TABLE TRIPLEG(
TNUM    DECIMAL(10)     NOT NULL,
LEGNUM  DECIMAL(2)  NOT NULL,
DEPARTURE VARCHAR(30)   NOT NULL,     DESTINATION VARCHAR(30) NOT NULL,
    CONSTRAINT TRIPLEG_PKEY PRIMARY KEY (TNUM, LEGNUM),
    CONSTRAINT TRIPLEG_UNIQUE UNIQUE(TNUM, DEPARTURE, DESTINATION),
CONSTRAINT TRIPLEG_FKEY1 FOREIGN KEY (TNUM) REFERENCES TRIP(TNUM) );

INSERT INTO TRIPLEG VALUES( 1, 1, 'Sydney', 'Melbourne');
INSERT INTO TRIPLEG VALUES( 1, 2, 'Melbourne', 'Hobart');

1 个答案:

答案 0 :(得分:0)

尝试以下内容;)

SELECT DISTINCT CONCAT('TOTAL NUMBER OF LEGS IN A TRIP NUMBER ', T1.TNUM, ' IS NOT EQUAL TO THE LARGEST NUMBER OF LEG IN THE TRIP') AS 'ERR'
FROM TRIPLEG T1
GROUP BY T1.TNUM
HAVING COUNT(T1.TNUM) <> MAX(T1.LEGNUM)