如何使用通用复合主键从多个mysql表中获取数据

时间:2012-10-17 17:36:53

标签: mysql database database-design normalization

我在mysql中有15个具有不同列数的表。列数范围从225到250.我每天从15个不同的CSV文件导入数据。

所有表格都有19个常用列,特定日期的值相同,3个常用列的组合可以作为主键。

我需要从15个表中获取数据而不进行重复,以将其显示为报告。怎么做?

对于参考样本表结构如下:

<html>
<table cellpadding="0" cellspacing="auto" border="1" class="display" id="example" width="100%">
            <thead>
                <tr>
                        <th>Start time </th>
                        <th>End time </th>
                        <th>Query Granularity</th>
                        <th>RNC ID</th>
                        <th>Cell</th>
                        <th>Cellname</th>
                        <th>Access Success Rate Signalling (%)</th>
                        <th>Access Success Rate Speech (%)</th> 
                        <th>Access Success Rate PS (%)</th>
                        <th>Access Success Rate HS (%)</th>
                        <th>Call Drop Rate Speech (%)</th>
                        <th>Call Drop Rate PS (%)</th>
                        <th>Call Drop Rate HS (%)</th>
                        <th>HOSR Speech (%)</th>
                        <th>iRAT HOSR out Speech (%)</th>
                        <th>iRAT HOSR out PS R99 (%)</th>
                        <th>number of rab establishment success for speech</th> 
                        <th>number of rab establishment success for PS</th>
                        <th>number of rab establishment success for HS  </th>
                        <th>number of CS call drop  </th>


                </tr>
            </thead>

            <tbody>
</table>
</html>

1 个答案:

答案 0 :(得分:1)

如前所述,您需要使用公共列计数来进行UNION查询。使用SELECT DISTINCT&lt; columna columnb&gt;删除UNION语句周围的重复项。言。

select distinct columna columnb from    
(   
    select subcol0a columna, subcol0b columnb, '', '' from table0    
        union
    select subcol1a columna, subcol1b columnb, '', '' from table1 
 )  as query0
where <some condition>  

只是别名子查询中的各个列,以便它们在外部SELECT中收集时匹配。