------解决方案-------------------- select username,count(b.id) nid,count(c.id)pid
from user a inner join news b on a.id=b.userid
inner join pics c on a.id=c.userid
group by username
order by count(b.id)+count(c.id) ------解决方案-------------------- ISNULL(c.Pics_count)--->ISNULL(c.Pics_count,0)
select *
from
(
select a.*,ISNULL(b.News_count,0)+ISNULL(c.Pics_count,0) as total
from [User] a
left join (select UserID,COUNT(*) as News_count from News group by UserID)b on a.ID=b.UserID
left join (select UserID,COUNT(*) as Pics_count from Pics group by UserID)c on a.ID=c.UserID
)t
order by total desc
------解决方案--------------------
;WITH A AS
(
SELECT COUNT(1) AS cnt,UserID
FROM
(
SELECT UserID
FROM News
UNION ALL
SELECT UserID
FROM Pics
) T
GROUP BY UserID
)
SELECT U.*,ISNULL(A.cnt,0) AS [Sum]
FROM [User] U LEFT JOIN A ON U.ID=A.UserID
ORDER BY [Sum] DESC
------解决方案-------------------- select * from User a
order by (select count(*) from News where userId=a.id)+
(select count(*) from pics where userId=a.id) desc ------解决方案-------------------- 低版本的access可能不支持left join
这是网上找的
Access不支持两个以上的 left join 在网上查一下,有哥们说每个 left join 要加 ()
在修改之前(不能执行,ms sql 里执行以通过)
select table1.fildes1, table1.fildes2,table2.fildes1, table2.fildes2,table3.fildes1, table3.fildes2 from
table1 left join table2 on table1.fildes1= table2.fildes1 left join table3 on table1.fildes1= table3.fildes1
修改之后(执行通过) 每个 left join 要加 ()
select table1.fildes1, table1.fildes2,table2.fildes1, table2.fildes2,table3.fildes1, table3.fildes2 from
((table1 left join table2 on table1.fildes1= table2.fildes1) left join table3 on table1.fildes1= table3.fildes1 ------解决方案--------------------