按第二个查询划分

时间:2013-05-23 00:18:47

标签: mysql sql

我试图先通过第二个查询,我该怎么做呢?

SELECT COUNT(*) AS Result
FROM pparsdb.application
    INNER JOIN pparsdb.planning_scheme
    ON application.planning_scheme = planning_scheme.ps_code
    WHERE planning_scheme.markus_ra = 'GA'

SELECT Result / COUNT(*)
FROM pparsdb.planning_scheme 
WHERE markus_ra = 'GA'

2 个答案:

答案 0 :(得分:2)

试试这个,

SELECT  COUNT(*) / crss.totalCount AS Result
FROM    pparsdb.application
        INNER JOIN pparsdb.planning_scheme
            ON application.planning_scheme = planning_scheme.ps_code
        CROSS JOIN
        (
            SELECT  COUNT(*) totalCount
            FROM    pparsdb.planning_scheme 
            WHERE   markus_ra = 'GA'
        ) crss
WHERE   planning_scheme.markus_ra = 'GA'

答案 1 :(得分:2)

假设每个应用程序只有一个规划方案,这里的方法只使用一个没有子查询的查询:

SELECT COUNT(a.planning_scheme) / COUNT(*) AS Result
FROM pparsdb.planning_scheme ps left outer join
     pparsdb.application a
     ON a.planning_scheme = ps.ps_code
WHERE ps.markus_ra = 'GA'

即使该条件不成立,您也可以:

SELECT COUNT(a.planning_scheme) / COUNT(distinct ps.planning_scheme) AS Result
FROM pparsdb.planning_scheme ps left outer join
     pparsdb.application a
     ON a.planning_scheme = ps.ps_code
WHERE ps.markus_ra = 'GA'