先计算b表一个属性的平均分,然后插入a表中 。
UPDATE a SET a.perscore=(SELECT AVG(score) as avg
FROM B WHERE resourceid=32) FROM A a,B b
WHERE a.ID=b.resourceid AND b.resourceid=32;
点赞排行的一个数据库语句,输出排名前五的数据。
SELECT TOP
( 5 ) B.NickName,
B.HeadPhoto,
SUM ( GiveLike ) AS GiveLikeAll,
COUNT ( A.ResourceTitle ) AS ResourceAll
FROM
A,
B
WHERE
A.UploaderID= B.NickName
GROUP BY
B.NickName,
B.HeadPhoto
HAVING
SUM ( GiveLike ) > -1
AND COUNT ( A.ResourceTitle ) > -1
ORDER BY
GiveLikeAll DESC;
联合查询,进行分页查询
SELECT TOP
( 2) *,
( SELECT COUNT ( ID ) FROM A WHERE name = 'Summer_worm' ) AS COUNT
FROM
A
WHERE
name = 'Summer_worm'
AND ID NOT IN (
SELECT TOP
( 2 * 2 ) ID
FROM
A
WHERE
name = 'Summer_worm');
可能感兴趣的话题,随机5条
SELECT TOP
( 4 ) A.ID ,
B.NickName,
B.HeadPhoto,
A.Tag,
A.Tag2,
A.Tag3,
A.ResourceImgUrl
FROM
A,
B
WHERE
A.Tag= ( SELECT Tag FROM A WHERE ID = 27 )
AND (A.Tag2= ( SELECT Tag2 FROM A WHERE ID = 27 )
OR A.Tag3= ( SELECT Tag3 FROM A WHERE ID = 27 ))
AND A.UploaderID= B.NickName
ORDER BY
NEWID();