2012-08-11 23 views
0
category table 

----------------------- 
catid | catname 
------------------------- 

Deal table 
------------------------ 
dealid | name | catid | 
----------------------- 


Cement Table 
----------------------- 
cid | cname | catid 
----------------------- 

ordertable 
--------------------- 
oid | oname | catid 
--------------------- 

上面的三個表使用我想要得到CATI的總數和使用順序通過desc clause.how寫入SQL查詢?如何獲得三個表中的類別數?

the result like this way 

catid catcount catname 
----------------------- 
1  20  xxx 
2  19  YYY 
+0

請出示你要拿出結果的樣品。 – 2012-08-11 11:05:21

回答

1

你可以使用一個union創建一個包含所有類型類別的子查詢:

select c.name 
,  count(*) as TotalCount 
from (
     select catid 
     from deal 
     union all 
     select catid 
     from comment 
     union all 
     select catid 
     from ordertable 
     ) as lst 
join category c 
on  c.catid = lst.catid 
group by 
     c.name 
order by 
     count(*) desc 
+0

謝謝你,它工作良好。 – naveen 2012-08-11 11:13:05

相關問題