2

从 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 回答 1

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 演示

于 2013-08-09T05:33:18.933 回答