求一sql语句。解决办法
求一sql语句。。。
一个问答的数据,有三个表
user

question

checked是否通过审核
overid 是 表answer的id,表明该回答是问题的最佳回答
pubid是user的id
answer

现在我只会在一个表里查询
比如7天内发表问题总数排名的前三甲
得到

说明id为5的user7天内发布了45条问题
现在想查询
7天内(只要审核了就可以执行积分规则)
积分的排行(倒序)
积分规则:
1. 发布问题/回答被审核了 10分
2. 回答 是 最佳答案 20分
想得到类似
一条sql能做到吗?
不知道怎么多表联合查询,求助。
------解决方案--------------------
最佳答案加10分是因为回答已经加了10分,如果这个理解错误自己修改下
------解决方案--------------------
一个问答的数据,有三个表
user
question
checked是否通过审核
overid 是 表answer的id,表明该回答是问题的最佳回答
pubid是user的id
answer
现在我只会在一个表里查询
比如7天内发表问题总数排名的前三甲
select top 3
count(0) c, pubid
from
zsk_question
where
checked = 1
and datediff(d, pubtime, getdate()) <= 7
group by pubid order by c desc
得到
说明id为5的user7天内发布了45条问题
现在想查询
7天内(只要审核了就可以执行积分规则)
积分的排行(倒序)
积分规则:
1. 发布问题/回答被审核了 10分
2. 回答 是 最佳答案 20分
想得到类似
select nick(昵称), jifen(7天内的积分), count_question(7天内的问题数), count_answer(7天内的回答数), count_best(7天内的最佳答案)
一条sql能做到吗?
不知道怎么多表联合查询,求助。
------解决方案--------------------
SELECT pubid ,SUM([count_question(7天内的问题数)] ) AS [count_question(7天内的问题数)]
,SUM([count_answer(7天内的回答数)]) AS [count_answer(7天内的回答数)]
,SUM([count_best(7天内的最佳答案)]) AS [count_best(7天内的最佳答案)]
,SUM([jifen(7天内的积分)]) AS [jifen(7天内的积分)]
FROM (
SELECT pubid ,1 AS [count_question(7天内的问题数)],0 AS [count_answer(7天内的回答数)],0 AS [count_best(7天内的最佳答案)]
, 10 AS [jifen(7天内的积分)]
from question
where checked = 1
and datediff(d, pubtime, getdate()) <= 7
UNION ALL
SELECT A.pubid ,0 AS [count_question(7天内的问题数)],0 AS [count_answer(7天内的回答数)],1 AS [count_best(7天内的最佳答案)]
,10 [jifen(7天内的积分)]
from answer A INNER JOIN question Q
ON A.ID = Q.overid
where A.checked = 1
and datediff(d, A.pubtime, getdate()) <= 7
UNION ALL
SELECT pubid ,0 AS [count_question(7天内的问题数)],1 AS [count_answer(7天内的回答数)],0 AS [count_best(7天内的最佳答案)]
, 10 AS [jifen(7天内的积分)]
from answer
where checked = 1
and datediff(d, pubtime, getdate()) <= 7
) AS T
GROUP BY pubid
最佳答案加10分是因为回答已经加了10分,如果这个理解错误自己修改下
------解决方案--------------------
USE test
GO
---->生成表User
--
--if object_id('User') is not null
-- drop table [User]
--Go
--Create table [User]([id] int,[nick] nvarchar(50),[jifen] int)
--Insert into [User]
--Select 1,'test','5'
--Union all Select 2,'test2','10'
---->生成表question
--
--if object_id('question') is not null
-- drop table question
--Go
--Create table question([id] int,[checked] bit,[overid] int,[pubid] int,[title] nvarchar(50),[content] nvarchar(400),[pubtime] DATETIME)