我试图通过一个mysql查询获取回复的数量并且没有来自消息的回复。
例如,我发送"嘿嘿呀?"对于20个人,我想知道我收到了多少回复以及有多少回复。
我有一个表Thread和查找表Message。
线程表有列:
1.threadId (varchar)
2.messageGuid (varchar)
3.messageLookupId (int)
4.fromId (varchar)
5.toId (varchar)
6.replied (tinyint(1))
7.dateSent (timestamp)
消息表包含列:
1.id (int)
2.message (text)
线程和消息表可以通过Thread.messageLookupId = Message.id
我想要一个包含列的结果表:
message | totalSent | replied | notReplied | repliedPercentage | notRepliedPercentage
我已经尝试过
SELECT Message.message, count(Message.id) replied
FROM Thread
JOIN Message
ON Thread.messageLookupId = Message.id
WHERE replied LIKE 1
Group by Message.id
ORDER BY replied DESC
回复= 0表示没有回复
replied = 1表示已回复
这只会返回回复的号码,但是我很难获得我想要的其他列:
totalSent | notReplied | repliedPercentage | notRepliedPercentage
repliedPercentage和notRepliedPercentage将返回已回复的百分比而不是已回复
有什么想法吗?
答案 0 :(得分:1)
您可以使用回复的列值进行求和。
SELECT Message.id, sum( replied) as replied,
sum(replied=0) as notReplied,
sum(replied)/count(message.id)*100 repliedPercentage,
sum(replied=0)/count(message.id)*100 as notRepliedPercentage
FROM Thread
JOIN Message
ON Thread.messageLookupId = Message.id
Group by Message.id
答案 1 :(得分:1)
试试这个。
SELECT Message.id,
Count(1) totalSent,
Count(CASE
WHEN replied = 1 THEN 1
END) AS replied,
Count(CASE
WHEN replied = 0 THEN 1
END) AS notReplied,
Count(CASE
WHEN replied = 1 THEN 1
END) / Count(1) * 100 repliedPercentage,
Count(CASE
WHEN replied = 0 THEN 1
END) / Count(1) * 100 AS notRepliedPercentage
FROM Thread
JOIN Message
ON Thread.messageLookupId = Message.id
GROUP BY Message.id