我有一个用于聊天应用程序的SQL数据库,并且我想查找哪个用户发送的邮件多于收到的邮件。
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));
我已经尝试了示例中的那个,但是它一直显示此消息
查询错误:错误:ER_NO_SUCH_TABLE:表'test.messages'不存在“”
我找不到我哪里错了
答案 0 :(得分:0)
您可以使用此查询,该查询在两个派生表中计算每个发件人已发送的消息数以及每个收件人已收到的消息数,然后将这些表@Path("InsertSconti")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String InsertCustomer(@HeaderParam("token") String token,@QueryParam("customer") String customer)
{
JOIN
}表基于与发件人或收件人匹配的Users
。然后,它只选择发送的邮件多于收到的用户:
uid
输出:
SELECT u.name, m1.sent, m2.received
FROM Users u
LEFT JOIN (SELECT sender, COUNT(*) AS sent
FROM Messages
GROUP BY sender) m1 ON m1.sender = u.uid
LEFT JOIN (SELECT recipient, COUNT(*) AS received
FROM Messages
GROUP BY recipient) m2 ON m2.recipient = u.uid
WHERE sent > received