getting data from three different table and inserting it into new table - mysql

I have three tables with contents, now i want to get them and add it into new table but am having this sql error "Column count doesn't match value count at row 1"
here is the sql query.
insert into compare_year(yeara,yearb,yearc,data)
SELECT yeara
FROM table_1
UNION ALL
SELECT yearb, data
FROM table_2
UNION ALL
SELECT yearc
FROM table_3
below is how i created the tables
create table table_1(id int primary key auto_increment,yeara varchar(100));
create table table_2(id int primary key auto_increment,yearb varchar(100),data varchar(100));
create table table_3(id int primary key auto_increment,yearc varchar(100));
my new table is now
create table compare_year(id int primary key auto_increment,yeara varchar(100),yearb varchar(100),yearc varchar(100),data varchar(100))
please can someone help me. thanks

Note:when you union select queries,the number of columns should be equal.
and also you cannot insert mutiple select columns into a single row of another.
My solution will be like:
if three table contain same id,then you can do like this
insert into compare_year(yeara,yearb,yearc,data)
SELECT T1.yeara,T2.yearb,T3.yearc,T2.data
FROM table_1 T1
left Join table_2 T2 on T2.Id = T1.Id
left Join table_3 T3 on T3.Id = T2.Id

It looks like what you want is a JOIN rather than a UNION. When you union two select statements, they must have the same number of fields in the SELECT. For example,
insert into compare_year(yeara)
SELECT yeara
FROM table_1
UNION ALL
SELECT yearb AS yeara
FROM table_2
UNION ALL
SELECT yearc AS yeara
FROM table_3
would be acceptable syntactically. If you want to join the tables,
INSERT INTO compare_year(yeara, yearb, yearc, data)
SELECT table_1.yeara, table_2.yearb, table_3.yearc, table_2.data
FROM table_1, table_2, table_3
but note that this is full cartesian product of the tables. It's likely you want some conditionals as well in a WHERE clause. It's also worth noting that the order of the select cause is what's important for the INSERT, not the field names.

Related

How can I insert new rows in table 2 to table 1 comparing the ID?

I would like to know how I can insert new rows in my table 1 of table 2. The idea is that by comparing the two tables if in the second one you do not find the same ID in table 1 this inserts the new data in table 1.
This is the two table and the idea I want to do:
Tabla 1
ID-Name-Surname
1-Oriol-Molina
Tabla 2
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
And the result would be this:
Tabla 1
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
Tabla 2
ID-Name-Surname
1-Oriol-Molina
2-Ricard-Martin
Use the database to enforce data integrity. That is, if you don't want duplicate ids in the table, then declare a unique index/constraint:
create unique index unq_table1_id on table1(id);
Then, in MySQL, you can use on duplicate key update:
insert into table1 (id, name, surname)
select id, name, surname
from table2
on duplicate key update id = values(id);
The final statement is a no-op -- it does nothing except prevent an error.
The advantage of this approach is that the database will ensure that id is unique for any statement that inserts data into the table, not just this one.
You can use INSERT INTO .. SELECT with LEFT JOIN and IS NULL check, to fetch only those rows from Table2 which do no exist in Table1
INSERT INTO Table1 (ID, Name, Surname)
SELECT t2.ID, t2.Name, t2.Surname
FROM Table2 t2
LEFT JOIN Table1 t1 ON t1.ID = t2.ID
WHERE t1.ID IS NULL
You can try using a left join
insert into table1
select id, name, surname from table2 left join table1 on table2.id=table1.id
where table1.id is null

MySQL: Use a single Column to show as multiple select column in single row

Consider these example tables
E_id in Table1 is a primary key. From and Assign_to are foreign keys referenced with E_id.
I want to show a table like this:
I am not sure how I can implement it. Please share the SQL query which returns the desired table.
You could JOIN to Table1 twice:
SELECT
t2.work_name,
t1f.E_name AS `From`,
t1a.E_Name AS `Assign_to`
FROM Table2 t2
INNER JOIN Table1 t1f
ON t1f.E_id = t2.`from`
INNER JOIN Table1 t1a
ON t1a.E_id =t2.Assign_to
You can solve that problem with a simple temp table. It is not the most sophisticated way to solve it, but the solution is easy to comprehend.
The step are as followed:
Create a table with all data from table2
Add 2 columns to that table to store the name values for from and Assign_to
Update the columns with the name values from table1
Select your Data
MySQL-Code
-- create temp-table
CREATE TABLE table2_temp
SELECT * FROM table2;
-- add columns to enrich table with E_name from table1
ALTER TABLE table2_temp
ADD COLUMN E_name_from VARCHAR (125),
ADD COLUMN E_name_assign_to VARCHAR (125);
-- update temp-table with names from table1
-- for E_name_from
UPDATE table2_temp A
INNER JOIN table1 B ON (A.`from` = E_id)
SET A.E_name_from = B.E_name;
-- for E_name_assign_to
UPDATE table2_temp A
INNER JOIN table1.B ON (A.Assign_to = E_id)
SET A.E_name_assign_to = B.E_name;
-- now you can select your date from the temp-table
SELECT
work_name,
E_name_from AS `From`,
E_name_assign_to AS `Assign_to`
FROM
table2_temp;
-- drop table after work is done
drop table if exists table2_temp ;

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

How to get different records from two tables with different columns using sql

I am having problem with SQL. I want to get the different records from two different tables with different number of columns, which have the same primary key.
I have tried "EXCEPT" and "MINUS", but since the two tables have different number of columns, I received errors.
Is there any other sql commands that I can use to get the records that I want?
Thanks,
Min
You can use INNER JOIN Like:
SELECT table1.column1, table2.column3
FROM table1 INNER JOIN table2
ON table1.column1=table2.column1;
A FULL OUTER JOIN with key columns equal to NULL conditions can help you solve your issue.
Here is the sample SELECT statement
/*
create table Tab1(id int, col1 varchar(10))
create table Tab2(id int, col2 varchar(10), col3 int)
insert into tab1 select 1,'Kodyaz'
insert into tab1 select 2,'Vader'
insert into tab2 select 2,'Star Wars', 6
insert into tab2 select 3,'SQL', 2014
*/
select *
from tab1
full outer join tab2 on tab1.id = tab2.id
where
tab1.id is null
or tab2.id is null
I hope it helps,
Try this:
SELECT t1.table1column1,
t1.table1column2,
t1.table1column3,
t2.table2column1,
t2.table2column2,
t2.table2column3
FROM table1 t1
JOIN table2 t2 ON t1.table1column1 = t2.table2column1

Alternative to in operator in mysql

I see In operator alternative in mysql
I have nearly 25,000 ids.I am using in operator on that.Then i am getting Stackoverflow Exception.Is there any other alternative for IN operator in mysql.
Thanks in advance..
If the ID's are in another table:
SELECT * FROM table1 WHERE id IN (SELECT id FROM table2);
then you can use a join instead:
SELECT table1.* FROM table1 INNER JOIN table2 ON table1.id = table2.id;
You could do the following:
1 - Create a MySQL Temporary Table
CREATE TEMPORARY TABLE tempIdTable (id int unsigned not null primary key);
2 - Insert All Your ids into the Temporary Table
For every id in your list:
insert ignore into myId (id) values (anId);
(this will have the added bonus of de-duplicating your list of ids ready for the final step)
3 - Join Against the Temporary Table
SELECT t1.* FROM myTable1 t1 INNER JOIN tempIdTable tt ON t1.id = tt.id;
The temporary table will disappear as soon as your connection is dropped so your don't have to worry about dropping it before you create it next time.