【问题标题】:how to fetch data from multiple mysql tables with common composite primary key如何从具有公共复合主键的多个mysql表中获取数据
【发布时间】:2012-10-08 01:14:27
【问题描述】:

我在 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>

【问题讨论】:

  • 在将 3 个公共列作为复合主键后尝试对所有表进行联合。但由于表中的列不同,它不起作用。请建议。
  • 为了使联合工作,所有选择语句必须具有相同数量的字段名称
  • 完全正确,Ertunc。谢谢你的建议。我试图将三个作为复合主键。但不能对多个表使用复合主键的组合。如果您有任何想法,请提出建议。
  • 要使用联合,您可以使用空白/虚拟列来填充您的查询,以便每个具有相同数量的列。例如。 select '' as dummycol

标签: mysql database database-design normalization


【解决方案1】:

如前所述,您需要有一个共同的列数才能进行 UNION 查询。要删除 UNION 语句周围的重复项,请使用 SELECT DISTINCT 语句。

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 中收集时匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2012-12-31
    • 1970-01-01
    • 2015-02-22
    • 2011-11-03
    • 2012-10-25
    • 2015-07-26
    相关资源
    最近更新 更多