所以我正在修改考试,并在SQL河(或废弃地)中敲了一块大石头
我制作了下表并插入了以下数据:
create table Permissions
(
fileName VARCHAR(40),
userID VARCHAR (16),
type VARCHAR(10),
startdate DATE,
duration NUMBER (5),
constraint Pri_key PRIMARY KEY (userID,fileName)
);
create table Files
(
name VARCHAR(20),
fsize INT,
numberofpermissions INT,
constraints PRI_KEY2 PRIMARY KEY (name)
);
create table Users
(
id VARCHAR(20),
password VARCHAR (20),
constraint Pri_key3 PRIMARY KEY (id)
);
-- after all tables create:
alter table Permissions
add constraint Forn_key FOREIGN KEY (userID) REFERENCES Users(id)
INITIALLY DEFERRED DEFERRABLE;
alter table Permissions
add constraint Forn_key2 FOREIGN KEY (filename) REFERENCES Files(name)
INITIALLY DEFERRED DEFERRABLE;
insert into Permissions VALUES ('Agenda','Jones','read','19-JAN-10',30);
insert into Permissions VALUES ('Agenda','Chun','read','19-JAN-10',30);
insert into Permissions VALUES ('Agenda','Rashid','write','17-JAN-10',50);
insert into Permissions VALUES ('Finance','Chun','write','05-DEC-09',50);
insert into Permissions VALUES ('AnnualReport','Jones','write','12-DEC-09',50);
insert into Users VALUES ('Jones', 'duck');
insert into Users VALUES ('Chun', 'tiger');
insert into Users VALUES ('Adams', 'shark');
insert into Users VALUES ('Rashid', 'puma');
insert into Files VALUES ('Agenda', 32, 3);
insert into Files VALUES ('FinanceTables',645, 0);
insert into Files VALUES ('Finance', 120, 1);
insert into Files VALUES ('AnnualReport', 1205, 1);
commit;
我现在正在尝试编写一个SQL命令来为每个拥有的用户显示 总大小超过50的文件的权限:用户的权限 id,用户拥有权限的所有文件的总大小,以及 用户的密码。
这是我到目前为止所做的,但是当我尝试添加任何内容来获取密码时,SQL +会出现一个异乎寻常的问题,我的屏幕上很快就会出现漏洞!
SELECT permissions.userID, sum(fsize) AS Totalsize
FROM files, permissions
where permissions.filename = files.name
group by permissions.userid
having SUM(fsize) > 50;
答案 0 :(得分:8)
在oracle中,您需要通过
指定整个组SELECT permissions.userID, users.password, sum(fsize) AS Totalsize
FROM files, permissions, users
where permissions.filename = files.name
and users.id = permissions.userID
group by permissions.userid, permissions.password
having SUM(fsize) > 50;
这与MySQL不同,其中可以隐含group by,但这更为正确。
答案 1 :(得分:5)
加入用户表,并将密码添加到group by子句:
SELECT permissions.userID, users.password, sum(fsize) AS Totalsize
FROM files, permissions, users
where permissions.filename = files.name
and users.id = permissions.userID
group by permissions.userid, users.password
having SUM(fsize) > 50;
答案 2 :(得分:4)
使用JOIN语法。它符合ANSI SQL。使用WHERE加入是一种不应再使用的旧语法。
SELECT
u.id AS userid, u.password, SUM(f.fsize) AS Totalsize
FROM
users u
INNER JOIN permissions p
ON u.id = p.userID
INNER JOIN files f
ON p.filename = f.name
GROUP BY
u.id, u.password
HAVING
SUM(f.fsize) > 50;
请注意,基于聚合函数的条件必须放在HAVING子句中。 WHERE和HAVING子句之间的区别在于,在分组之前执行WHERE并在分组之后执行HAVING。
此外,GROUP BY子句必须包含SELECT列表中没有聚合函数的所有表达式。
答案 3 :(得分:1)
这应该有效:
SELECT Users.id, Users.password, Totalsize
FROM Users
INNER JOIN
(SELECT userId, SUM(fSize) AS TotalSize
FROM permissions
INNER JOIN files ON permissions.filename = files.name
group by permissions.userid
having SUM(fsize) > 50) t ON t.userId = Users.Id
当然,这可以在没有内部连接的情况下完成,但是如果您需要更多字段,则可以更轻松地添加它们,而无需将额外字段添加到group by子句中。