用于创建视图的mysql查询

问题描述 投票:1回答:1

这是我的代码:

create DATABASE assignment

CREATE TABLE Books (
    id INTEGER PRIMARY KEY,
    category TEXT,
    price FLOAT CHECK (price>0), 
    promoted bit DEFAULT 1
);

INSERT INTO Books (id, category, price) VALUES (1, 'Dictionary', 100); 
INSERT INTO Books (id, category, price) VALUES (2, 'Dictionary', 150); 
INSERT INTO Books (id, category, price) VALUES (3, 'Science', 120); 
INSERT INTO Books (id, category, price) VALUES (4, 'Science', 190); 
INSERT INTO Books  (id, category, price) VALUES (5, 'Science', 320);

CREATE VIEW PromotionSummary AS
SELECT category, MIN(price) AS minprice , MAX(price) AS maxprice 
FROM Books
WHERE promoted
GROUP BY category;

面对此错误:

Msg 4145,第15级,状态1,程序升级摘要,第5行在需要“ GROUP”附近的条件中指定的非布尔类型的表达式。

我现在正在使用azure数据库。

sql sql-server database tsql sql-view
1个回答
0
投票

错误消息表明您正在使用SQL Server,而不是MySQL。如果是这样,则需要在promoted的谓词上输入一个值(MySQL会允许该值,但SQL Server不允许)。想必您想要:

CREATE VIEW PromotionSummary AS 
SELECT category, MIN(price) AS minprice , MAX(price) AS maxprice 
FROM Books 
WHERE promoted = 1 
GROUP BY category;
© www.soinside.com 2019 - 2024. All rights reserved.