使用连接合并两个mySQL查询

时间:2015-03-11 23:51:22

标签: mysql

我知道我需要为此做一些子查询,但我无法让它工作。

查询1:

SELECT full_db3.dma, dma_list.dma_name 
from `full_db3` 
inner join dma_list on full_db3.dma = dma_list.dma;

查询2:

SELECT dma, COUNT(*) as dma_count, 
    round(COUNT(*)/(SELECT COUNT(*) FROM full_db3) * 100,2) as dma_percent 
FROM full_db3 
where dma != '0' 
GROUP BY dma 
ORDER BY dma_count DESC;

第一个查询将dma编号与dma名称匹配。我需要的是将此dma名称添加到查询返回的内容中作为查询2的结果,以便给我:dma, dma_name, dma_percent

将这些结合起来的最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

只需将JOIN dma_list添加到第二个查询:

SELECT full_db3.dma, dma_list.dma_name, COUNT(*) as dma_count, 
    round(COUNT(*)/(SELECT COUNT(*) FROM full_db3) * 100,2) as dma_percent 
FROM full_db3 
INNER JOIN dma_list on full_db3.dma = dma_list.dma
where full_db3.dma != '0' 
GROUP BY full_db3.dma 
ORDER BY dma_count DESC;