简单的SQL查询,结合结果和划分

时间:2010-08-19 12:54:15

标签: sql mysql

我正试图从 2个表中获取 2个计数并计算出与MySQL数据库相同的百分比:

  1. select field_one, count(*) as COUNT_ONE from table1 group by field_one;

  2. select other_field,count(*) as COUNT_TWO from table2 group by other_field;

  3. 我想要合并结果并获得FINAL_COUNT=(COUNT_ONE/COUNT_TWO) * 100百分比?

2 个答案:

答案 0 :(得分:17)

快速而肮脏:

select (a.count_one / b.count_two) * 100 as final_count from 
(select field_one, count(*) as count_one from table1 group by field_one) a,
(select field_two, count(*) as count_two from table2 group by field_two) b
where a.field_one = b.field_two

答案 1 :(得分:5)

select sum(((QUERY FROM TABLE 1) / (QUERY FROM TABLE 2)) * 100) as percentage