有一个mysql查询的问题来创建视图

时间:2013-08-09 04:56:41

标签: mysql

从mysql开始并努力解决这个问题,我将非常感谢帮助

我有一个像这样的软件销售示例表,id会自动递增,条目会在下载时生成

 ID | App name   | platform | release date |downloadDate |price
====+============+==========+=============+==============+====== 
1   | calculator | windows  | 2013-03-01  | 2013-01-21   | $10 
2   | text editor| mac      | 2013-04-07  | 2013-02-24   | $8 
3   | mp3 player | mac      | 2013-01-23  | 2013-01-12   | $12 
4   | calendar   | linux    | 2012-08-15  | 2013-04-14   | $5 
5   | mp3 player | windows  |2013-02-11   | 2013-03-15   | $12 
6   | text editor| linux    |2012-10-13   | 2013-03-22   | $8 
7   | calculator | mac      |2013-04-24   | 2013-05-17   | $10 
8   | mp3 player | linux    |2013-04-16   | 2013-07-03   | $12 
9   | text editor| linux    |2013-03-22   | 2013-06-12   | $8 
10  | calendar   | mac      |2012-08-15   | 2013-04-14   | $5

我要做的是创建3个视图来显示数据

  1. 显示应用程序名称以及该应用程序和订单的所有平台的总数,然后降序 x将是所有平台的总数(win / mac / linux)

    离。

    App name   | total
    ===========+======
    calculator | x
    mp3 player | x
    text editor| x
    
  2. 视图显示每个平台和订单的应用名称和总下载量,然后降序

    离。

    App name   |platform |total
    ===========+=========+=====
    calculator | windows | 12
    calculator | mac     | 6
    calculator | linux   | 2
    
  3. 显示应用程序名称以及该应用程序和订单的所有平台的总数,然后按价格下降

    离。

    App name   | total |price
    ===========+=======+====
    calculator | x     |$10
    mp3 player | x     |$12
    text editor| x     |$8
    
  4. 我已经找到了一个答案,但找不到一个非常接近的案例,这是一个有点长篇大论的帖子,但它是最好的,我可以解释一下?

1 个答案:

答案 0 :(得分:0)

这些是您可能需要的视图:

CREATE VIEW App AS
SELECT appname, COUNT(*) as Total FROM softwaresales
GROUP BY appname
ORDER BY Total DESC;

CREATE VIEW AppPlatform AS
SELECT appname, platform, COUNT(*) as Total FROM softwaresales
GROUP BY appname, platform
ORDER BY Total DESC;


CREATE VIEW AppPrice AS
SELECT appname, COUNT(*), price FROM softwaresales
GROUP BY appname
ORDER BY price DESC;

你可以这样称呼他们:

SELECT * FROM App;
SELECT * FROM AppPlatform WHERE appname ='calculator';
SELECT * FROM AppPrice;

检查我的sqlfiddle demo