sqlquery没有工作给出错误

时间:2014-03-01 02:58:14

标签: mysql sql database

我为图书数据库编写查询。问题是按降序列出1995-2000每位作者的总收入。

SELECT DISTINCT b.author,b.earnings as TOTAL_EARNING
FROM BookAuthor as b lEFT JOIN
     BookPublish
     on b.book=BookPublish.book
WHERE (b.earnings=COUNT(b.earnings)) AND (year BETWEEN 1995 AND 2000)
ORDER BY b.earnings desc ;

ERor:无效使用群组功能。

create table BookPublish( 
      book varchar(255) ,
      year int,
      publisher varchar(255),
      price int,
      num int,
      PRIMARY KEY (book)
    );

    //INSERT INTO BookPublish VALUES('Math-1',1999,'Mcgraw',120,5000);
    //INSERT INTO BookPublish VALUES('Physics',2001,'Matt Woods',85,2500);
    //INSERT INTO BookPublish VALUES('Chemistry',1996,'McGraw',99,2300);
    //INSERT INTO BookPublish VALUES('Biology',1999,'Krishna',75,2000);
    //INSERT INTO BookPublish VALUES('Economics',2000,'Maze',125,3000);
    //INSERT INTO BookPublish VALUES('Engineering',1994,'ABC',180,8000);
    //INSERT INTO BookPublish VALUES('Medicine',1997,'ABC',180,9000);


    create table BookAuthor( 
      book varchar(255) ,
      author varchar(255) ,
      earnings int,
      PRIMARY KEY (book,author)
    );


    //INSERT INTO BookAuthor VALUES('Math-1','Chris,Norman',12000);
    //INSERT INTO BookAuthor VALUES('Physics','Matt Woods',4000);
    //INSERT INTO BookAuthor VALUES('Chemistry','John Bayer',8200);
    //INSERT INTO BookAuthor VALUES('Biology','Craig S',3000);
    //INSERT INTO BookAuthor VALUES('Economics','Manohar',17000);
    //INSERT INTO BookAuthor VALUES('Engineering','Sterling',13050);
    //INSERT INTO BookAuthor VALUES('Medicine','Craig S',10000);

    create table BookReview( 
      book varchar(255) ,
      reviewer varchar(255) ,
      score int,
      PRIMARY KEY (book,reviewer)  ,
      FOREIGN KEY (book) REFERENCES BookPublish(book)
    );


    //INSERT INTO BookReview VALUES('Math-1','Paul Gray',7);
    //INSERT INTO BookReview VALUES('Math-1','Daphne Merkin',8);
    //INSERT INTO BookReview VALUES('Physics','Daphne Merkin',6);
    //INSERT INTO BookReview VALUES('Chemistry','John',9);
    //INSERT INTO BookReview VALUES('Biology','Jack',10);
    //INSERT INTO BookReview VALUES('Economics','Paul Gray',7.5);
    //INSERT INTO BookReview VALUES('Engineering','Paul Gray',8);
    //INSERT INTO BookReview VALUES('Engineering','Daphne Merkin',7);


    create table BookReference( 
      book varchar(255) ,
      bookreferenced varchar(255) ,
      PRIMARY KEY (book, bookreferenced)  ,
      FOREIGN KEY (book) REFERENCES BookPublish(book)
    );

    //INSERT INTO BookReference VALUES('Math-1','Introduction to Math');
    //INSERT INTO BookReference VALUES('Physics','Foundation of Physics');
    //INSERT INTO BookReference VALUES('Chemistry','Introduction to Chemistry');
    //INSERT INTO BookReference VALUES('Biology','Molecular Biology');
    //INSERT INTO BookReference VALUES('Economics','Micro-Economics');
    //INSERT INTO BookReference VALUES('Engineering','Engineering-1');

2 个答案:

答案 0 :(得分:3)

您在where子句中有这个:

 b.earnings = COUNT(b.earnings)

您不能混合聚合和非聚合值。

你想要的是group by

SELECT ba.author, sum(ba.earnings) as TOTAL_EARNING
FROM BookAuthor as ba lEFT JOIN
     BookPublish bp
     on ba.book = bp.book
WHERE year BETWEEN 1995 AND 2000
GROUP BY ba.author
ORDER BY sum(ba.earnings) desc ;

答案 1 :(得分:0)

由于您需要收入,因此每个列乘以BookPublish列的ba.earning将保持相同,因此总数将变为错误。因此,它应该保持只是ba.earnings没有任何方法。如果您想根据BookPublish计算收入,下面的内容应该有效

SELECT b.author, b.book AS book, b.earnings AS TOTAL_EARNING, sum(bp.num) * bp.price AS EARNING
FROM BookAuthor b
LEFT JOIN BookPublish bp ON b.book=bp.book
WHERE year BETWEEN 1995 AND 2000
GROUP BY b.author, b.book
ORDER BY b.earnings desc ;

在这里,我尝试了解销售的书籍数量和数量。乘以价格,GROUP BY仍为作者和书籍,因为每本出售的书都会有不同的价格。 通过这种方式,您将获得多个作者行,以后可以使用for循环在结果对象

中控制代码

在SQLFiddle上添加 - http://sqlfiddle.com/#!2/9755d/6

编辑 - 您可以考虑改进的表格结构

CREATE TABLE `AUTHORS`(
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `fname` TEXT NOT NULL,
    `lname` TEXT NOT NULL,
    `email` VARCHAR(255) NOT NULL
);
CREATE TABLE `PUBLISHERS` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` TEXT NOT NULL,
    `phone_no` VARCHAR(16) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `address` TEXT
);
CREATE TABLE `BOOKS` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `name` TEXT NOT NULL,
    `publish_year` INT NOT NULL,
    `author_id` INT NOT NULL,
    KEY `author_id` (`author_id`),
    CONSTRAINT `books_author_fk1` FOREIGN KEY `author_id` REFERENCES AUTHORS (`id`)
);
CREATE TABLE `BOOKPUBLISH` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `book_id` INT NOT NULL,
    `publisher_id` INT NOT NULL,
    `publish_year` INT NOT NULL,
    `price` INT NOT NULL,
    KEY `book_id` (`book_id`),
    CONSTRAINT `bookpublish_book_fk1` FOREIGN KEY `book_id` REFERENCES BOOKS (`id`),
    KEY `publisher_id` (`publisher_id`),
    CONSTRAINT `bookpublish_publisher_fk1` FOREIGN KEY `publisher_id` REFERENCES PUBLISHER (`id`)
);

这样你就可以定义你的其他表了,BOOKREVIEW& BOOKREFERENCE