MySQL: Combining multiple columns from Table 1 and inserting into 1 column in Table 2 - mysql

I've been trying to figure this out, but can't seem to come up with a simple solution.
Say for instance I have a table that has similar data throughout 3 columns (i.e. different types of activities spanning 3 columns) but I want to have those three columns inserted into a separate table (Table2) so I can keep the like data together and perform a JOIN to match it with its respective data in Table1.
I'm not talking about performing a CONCAT or CONCAT_WS, but moving those three columns from Table1 into one column in Table2, each item with its own row.
Is there a way to do this through a query without having to manually insert each entry into Table2?
Thank you in advance!

It might be as simple as:
insert into table2
(field)
select column1 from table1
union
select column2 from table1
union
select column3 from table1
But, before you do this, decide what you want to do if two columns in table1 have the same value.

Related

Merge 2 tables into a new table, when the tables have same set of columns but unordered

I have a MySQL database with 2 tables suppose table1 & table2 each having the same set of 300 columns.
Now what I want to do is create a new table (eg. table3) consisting of rows from table1 and table2.
There is no row matching needed between the two tables. I tried insert, union and various other operations but the catch is the columns are unordered inside the tables. But they have the same set of column names.
I want to merge the two tables with the rows having data in respective columns, which I am not able to do using UNION or INSERT operation.
You can always explicitly specify column names instead of insert into t values and select *:
insert into t3(col1, col2, col3, ...)
select col1, col2, col3, ... from t1
union all
select col1, col2, col3, ... from t2
After trying a lot of different ways to merge the tables using SQL commands I was unable to find any solution which fulfilled the requirements.
One way in which this can be solved using Python is to load the tables into a Pandas dataframe and then concat these two to a new dataframe.
Pandas merge them properly by matching the column names and then you can reload the table to the database.
Any other solution is appreciated.

how to create table with unique rows out of two tables

i have two tables, with same columns. but there are duplicates. i want to create third one but without duplicate rows. what is the best way to do this keeping in mind that tables have over million records?
Table have two columns, ean and price
If both the tables have same structure then , I think you should try union.
Sample Query--
Assuming 2 table from which the data needs to be retrieved as
table_src_1
table_src_2
Final Table-table_unique_records
Your Query-
create table table_unique_records as
(select * from table_src_1
union
select * from table_src_2)

Inserting data - Mysql workbench

How do you insert columns from two separate tables simultaneously into a third table in mysql? If i use two insert into queries, the data from the second query is put beneath the data from the first query, but i want them to appear on the same rows not one beneath the other.
Use mysql's insert ... select command and join the 2 source tables together in the select part of the query:
insert into target (col1, col2)
select source1.col1, source2.col2
from source1
inner join source2 on source1.id=source2.s1_id
This way data from the 2 source tables will appear in the same record within the target table.

Combining several columns without concatenate MySQL

I want to combine several columns from many tables into 1 column. However, I do not want to concatenate them. I want the values to be inserted into an empty fields below the already inserted values. Is there a way to do this without using a ton of SELECT statements?
For example, I have 4 columns from 13 tables. I want these put into one column in a new table.
Right now the only way I can figure out to do this is like this
INSERT INTO softwareid (software)
SELECT column1
FROM table1
UNION ALL
SELECT column1
FROM table2
UNION ALL
SELECT column1
FROM table3
UNION ALL
SELECT column1
FROM table4
Doing it this way means I need to create more SELECT statements for every column. Is there a way to combine all of that?

MySQL - how to insert values from one table into another with string matching?

MySQL noob here; looked around first but couldn't find the answer to this question.
So I have two tables in MySQL, one (Table1) which consists of one column containing unique list of names (Col1) and another containing corresponding binary values (Col2). The second table (Table2) contains a list of recurring names with a empty column waiting to be filled with binary values from the first table. What I want to do is, for each instance of a recurring name in Table2, insert the binary value from Col2 associated with the matching unique name in Table1.
I know how to do this in Excel—you just place the following VLOOKUP statement next to every row containing a recurring name. In the following code snippet A2 is a recurring name, unique names are contained in column B, and the binary values are contained in column C.
=VLOOKUP(A2,$B$2:$C$106095,2,FALSE)
But I can't for the life of me figure out how to reproduce this effect in MySQL. I can't use Excel because there's too much data. Anyone have any ideas? Thanks in advance!
I think that you want something like this (I don't know what the Excel statement does):
UPDATE table2 JOIN table1 ON table1.col1 = table2.col1
SET table2.col2 = table2.col2
WHERE table2.col2 IS NULL
This will update each row table2 that has col2 empty, searching for the corresponding row in table1 based on matching col1 columns.
Btw, do you have a reason to do this? Why not just join both tables when selecting the data? For example:
SELECT table2.col1, table1.col2
FROM table2 JOIN table1 ON table1.col1 = table2.col1