这是我正在使用的数据库
CREATE TABLE Users(
uid int PRIMARY KEY,
name text,
phone text
);
CREATE TABLE Messages(
recipient int REFERENCES Users(uid),
sender int REFERENCES Users(uid),
time timestamp NOT NULL,
message text NOT NULL,
PRIMARY KEY (recipient, sender, time)
);
我需要回答的查询是 “考虑已收到给用户“ SomeUser”的消息的所有用户。 还有多少其他用户已向所有 “ SomeUser”发送?”
我已经尝试过了,但是似乎不起作用,给出了count = 0(我想是因为我要我的数据库告诉我接收者(一个值)是否等于ALL创建的向量。 )
SELECT count(*)
FROM Users AS U , Messages AS M
WHERE U.uid = M.sender AND U.name != 'SomeUser' AND M.recipient =ALL
(#id of users that received a message from “SomeUser”
SELECT DISTINCT M.recipient
FROM Users AS U , Messages AS M
WHERE U.name='SomeUser' AND U.uid=M.sender
);
如果此数据库中有这些值
INSERT INTO Users VALUES (0,'Maria','1123456123');
INSERT INTO Users VALUES (1,'John','1123456123');
INSERT INTO Users VALUES (2,'Freddy','1123456123');
INSERT INTO Users VALUES (3,'Joanna','1123456123');
INSERT INTO Users VALUES (4,'Jim','1123456123');
INSERT INTO Users VALUES (5,'Luis','1123456123');
INSERT INTO Messages VALUES (0,1,'2012-12-15 12:57:59.99999','Hello there');
INSERT INTO Messages VALUES (0,2,'2012-12-15 21:59:59.9999','Hello there');
INSERT INTO Messages VALUES (0,3,'2001-12-15 20:57:50.999','Hello theret');
INSERT INTO Messages VALUES (1,4,'2012-12-15 11:59:59.999','Hello there');
INSERT INTO Messages VALUES (2,3,'2012-12-15 21:52:50.9999','Hello there');
INSERT INTO Messages VALUES (2,4,'2012-12-15 11:59:59.99999','Hello there');
INSERT INTO Messages VALUES (3,4,'2012-12-15 20:57:50.999999','Hello there');
INSERT INTO Messages VALUES (4,1,'2012-12-15 23:59:59.99999','Hello there');
INSERT INTO Messages VALUES (4,2,'2012-12-15 20:57:59.9999','Hello there');
INSERT INTO Messages VALUES (2,3,'2012-12-15 21:51:59.999','Hello there');
INSERT INTO Messages VALUES (1,4,'2012-12-15 20:51:59.9999','Hello there');
INSERT INTO Messages VALUES (3,2,'2012-12-15 22:59:50.99999','Hello there');
INSERT INTO Messages VALUES (3,2,'2012-12-15 15:59:59.9999','Hello there');
INSERT INTO Messages VALUES (3,4,'2012-12-15 23:22:59.9999','Hello there');
INSERT INTO Messages VALUES (3,0,'2012-12-15 23:01:59.9999','Hello there');
INSERT INTO Messages VALUES (1,5,'2012-12-15 23:01:59.9999','Hello there');
INSERT INTO Messages VALUES (2,5,'2012-12-15 23:01:59.9999','Hello there');
INSERT INTO Messages VALUES (3,1,'2001-12-15 23:59:59.99999','Hello there');
INSERT INTO Messages VALUES (5,1,'1995-12-15 23:59:59.99999','Hello there');
INSERT INTO Messages VALUES (3,5,'2002-12-15 23:01:59.9999','Hello there');
考虑SomeUser ='Luis',此查询需要返回 1(因为发送到uid 1,2和3的唯一一个是4) 如果SomeUser ='Maria',答案必须为4
提前感谢您的时间。
答案 0 :(得分:2)
分组,加入并最终计数:
select count(*) counter from (
select m.sender
from Messages m inner join Users u
on u.uid = m.sender
where
u.name <> 'Maria' and
m.recipient in (
select distinct m.recipient
from Messages m inner join Users u
on u.uid = m.sender
where u.name = 'Maria')
group by m.sender
having
count(distinct m.recipient) =
(select count(distinct m.recipient)
from Messages m inner join Users u
on u.uid = m.sender
where u.name = 'Maria')
) t
请参见demo。