我有三个表:applications、permissions 和 applications_permissions
|------------| |------------------------| |-----------|
|applications| |applications_permissions| |permissions|
|------------| |------------------------| |-----------|
| id | <-| application_id | | id |
| price | | permission_id |-> | name |
|------------| |------------------------| |-----------|
对于应用程序有两类:免费和商业应用程序(价格 = '0' 和价格!= '0')
现在我想知道每个权限有多少百分比的应用程序引用它;这对于这两个类别
自由:
id, percentage
1 , 20.0230
2 , 0.0000
3 , 0.0312
...
商业的:
id, percentage
1 , 18.0460
2 , 0.0000
3 , 0.0402
...
我已经制定了以下查询,但它不包括没有应用程序的权限 ID:/
SELECT (SELECT name FROM permissions WHERE id = applications_permissions.permission_id) AS "name",
100::float * COUNT(*)/(SELECT COUNT(name) FROM applications WHERE price = \'0\') AS "percent"
FROM applications, applications_permissions
WHERE applications.id = applications_permissions.application_id
AND applications.price = \'0\'
GROUP BY applications_permissions.permission_id
ORDER BY percent DESC')
我该怎么做呢?我已经尝试了几个小时(那个查询,misc JOINs)但它让我无法理解:/