3

有没有办法形成这样的查询:

Id | action | object 
-------------
1 | view | article1 
2 | view | article2 
3 | view | article1 
4 | order | article1 
5 | order | article1 
6 | order | article3 

并产生这个:

name     | views | orders
----------------------
article1 |   2   |   2
article2 |   1   |   0
article3 |   0   |   1

一种解决方案是使用两个选择,然后在代码中加入它们。有更好的吗?

问候 && 蒂亚

noircc

4

1 回答 1

3

也许是这样的:

SELECT
    SUM(CASE WHEN Table1.action='view' THEN 1 ELSE 0 END) AS views,
    SUM(CASE WHEN Table1.action='order' THEN 1 ELSE 0 END) AS orders,
    Table1.object 
FROM
    Table1
GROUP BY
    Table1.object 
于 2012-04-25T07:42:33.037 回答