How to merge multiple mysql tables in one?
I have a Mysql DB with 121 tables, with different structure, and I need to merge all of this tables in a 1 table.
There are 3 fields that are the same in all tables (email, base_name, location) but all other fields in some tables are the same, in others not.
Is there any wall to merge the tables and conservate all the fields (it doesn't matter if there is null fields)?
Thank you very much.
I don't know if I understood well, but I guess your looking for joining tables and not merging them (sorry if i missunderstud). I put you an example of a left join, but if you want to keep every record from every table you'll need a full outer join (you'll have to simulate it in mysql cause it's not implemented)
CREATE TABLE table_name AS ( SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.email=t2.email AND t1.base_name=t2.base_name AND t1.location=t2.location ... LEFT JOIN tableN tN ON tN-1.email=tN.email AND tN-1.base_name=tN.base_name AND tN-1.location=tN.location )
if you want to simulate a full outer join you should union like this:
SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.email=t2.email AND t1.base_name=t2.base_name AND t1.location=t2.location ... LEFT JOIN tableN tN ON tN-1.email=tN.email AND tN-1.base_name=tN.base_name AND tN-1.location=tN.location UNION SELECT * FROM tableN tN LEFT JOIN tableN-1 tN-1 ON tN.email=tN-1.email AND tN.base_name=tN-1.base_name AND tN-1.location=tN.location ... LEFT JOIN table1 t1 ON t2.email=t1.email AND t2.base_name=t1.base_name AND t1.location=t2.location
- A Linux, Apache, MySQL, PHP (LAMP) stack is a combination of open source software that is typically installed together to enable a server to host dynamic […]
- MySQL is an the most popular open-source database management system, in most of hosting company its has been bundle by LAMP(Linux, Apache, MySQL, PHP/Python/Perl) stack. Another alternative […]
- MySQL is the world’s most popular open-source database. Despite its powerful features, MySQL is simple to set up and easy to use. Below are some instructions […]