0
category  table 

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

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


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

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

以上三个表使用我想获取catid的总数并使用desc子句的顺序。如何编写sql查询?

the result like this way

catid  catcount catname
-----------------------
1       20        xxx
2       19        YYY 
4

1 回答 1

1

您可以使用 aunion创建包含所有类型类别的子查询:

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
于 2012-08-11T11:06:52.740 回答