简体   繁体   中英

how to fetch data from multiple mysql tables with common composite primary key

I have 15 tables with different column counts in mysql. Column count ranges from 225 to 250. I am importing data from 15 different CSV files on a daily basis.

All the tables have 19 common columns with the same values for particular day and a combination of 3 common columns can act as primary key.

I need to fetch the data from 15 tables without duplication to display it as a report. How to do this?

For the reference sample table structure is below:

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

As has been mentioned you'll need to have a common column count to make UNION query. To remove the duplicates surround the UNION statement with a SELECT DISTINCT <columna columnb > statement.

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>  

Just alias the individual columns in the subquery so they match up when collected in the outer SELECT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM