I am trying to move a complete row of records which includes BLOB from one table (TABLE1) to another table (TABLE2) in the same database using SQL. I have tried what little I know but it failed.
What I have already used which failed is:
INSERT INTO TABLE2
SELECT * FROM TABLE1
WHERE
staff_id = '0010002';
How should I do this instead?
Try:
INSERT INTO TABLE2 (colname1, colname2)
SELECT colename1, colename2
FROM TABLE1
WHERE staff_id = '0010002'
And for more details.http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
Related
I have a table with many columns in SQL Server and I have move part of data into MySQL. I made a view or function on the table in SQL Server and these two databases must be synced once a day through job. Because the data of this view may change every day.
View return a table with 3 columns: (char, varchar, varchar) that none of them are unique or primary key.
My solution is:
create a job
execute view on SQL Server
return result of view
create temp table with 3 column in MySQL
move result view from SQL Server to temp table
move records from temp table to new table one by one if not exist before
delete temp table
To transfer without using the temp table, I wanted to use below type of query but could not find the correct query. That's why I used the temp table:
insert into new_table
values (array of records) where record if not exist in new table.
And for the solution I mentioned above, I used the following query:
insert into new_table
select *
from temp_table
where not exist new_table.column = temp_table.column
Do you have a better suggestion that new records can be fetch and added to previous records?
It should look more like this:
insert into new_table
select *
from temp_table
where not exists (
select 1
from new_table
where new_table.column = temp_table.column
)
or maybe this:
insert into new_table
select *
from temp_table
where not exists (
select 1
from new_table
where new_table.column = temp_table.column
and new_table.column2 = temp_table.column2
and new_table.column3 = temp_table.column3
)
How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.
I have been trying to move some data which relates to a specific column from one table to another. They both have a matching objectID.
So what I am trying to do is:
TABLE 1
ObjectID
Field with Data
TABLE 2
ObjectID
FIELD with NEW column
So the object ID relate to each other. All it is I am trying to do is move the data from Table 1 to Table 2 with the new column.
I have tried following but cant seem to get it all working. Is there anything that can be suggested that may help or point in me the right direction.
update Table2 a
Set a.NewColumn = (Select *
From Table1 b WHERE a.OBJECTID = b.OBJECTID
)
Hm, maybe I don't get it but why don't you use a INSERT?
INSERT INTO TableB(...columns...)
SELECT ...columns...
FROM TableA
You can use join update syntax for this, need to make sure that the Table2 already has data and you are updating a new column in there from Table1
update Table2 t2
join Table1 t1 on t1.OBJECTID = t2.OBJECTID
set t2.NewColumn = t1.Field
You can write query like this.
INSERT INTO Table2(ObjectID,Field)
SELECT ObjectID,Field
FROM Table1.
And you can put any default value in extra column.
I want to write a Mysql query for the following scenario.
1.check if a table( ex: tableA) exists.
2.Check if data is there in the table.
3.If tableA exists and data is there in the table move all data to another table( ex: tableB) (tableB there in db and both tables are having same structure)
4.drop tableA
Is it possible to write a mysql query avoiding mysql stored procedure ?
I was able to do first three with the below query, but drop table was not possible.
Hope it helps!
There are two tables: table_1(old),table_2(new). Both have one column "ID".
insert into table_2(id) #inserts into table
select case when
(
select count(*) from table_1 a #checks if there are any records in the table
where exists #checks if table exists
(select 1 from table_1 b where a.id=b.id))>0 then
id
else 0 end
from table_1
I have two tables. table1 (source) and table2 (destination). I want to whole records in table2 to be copied to table1 if and only if the record is not already exists in table1. I use the following statement:
INSERT INTO test.dest
SELECT *
FROM test.source
WHERE NOT EXISTS(SELECT *
FROM test.dest
WHERE (source.DomainName=dest.hostnmae)
);
My question: Is there another better method to speed the process??