there is my code sample that deletes data from queue_db,I need code which inserts queue_db values to staff_db table.enter image description here
You can use SQL query
Copy all record one table to another
insert into staff_db select * from queue_db
This will transfer the data one table to another, later you can delete records conditionally from first table
If the columns names are different then
Copy all record one table to another with column name alias
insert into staff_tb(column1, column2, column3) select c as column1, b as column2, d as column3 from queue_db
where c,b,d are the columns in the second table
If you want to filter it with condition then add where at the end of the query with <tablename.columnname> = <value>
Copy all record one table to another without duplicates
To prevent duplicates, you can use this answer Prevent Duplicate I don't want repeat the same answer here
If you want copy those data into fresh new table like doing a backup then
Copy all record one table to a fresh new table
select * into newtable from queue_db
Then you run delete query for table which you copied from
Delete all records from a table
delete from queue_db
Related
I'm currently using the following to move/copy the entire table from one table to another, which works perfectly.
INSERT INTO archivedpks SELECT * FROM pks
However I want to be able to specify what rows I want to move from the pks table to the archivedpks table.
My table has an id column, I want to only move the rows with an id from 25 to 250
How would I modify the query to only work on those rows?
INSERT INTO archivedpks SELECT * FROM pks WHERE id>=25 and id<=250
i'm trying to insert some rows from one table to another and add last inserted id to first table:
INSERT INTO tableA (fooA, fooA2) SELECT fooB, fooB2 FROM tableB;
And now i want to add last inserted id into tableB for each row
UPDATE tableB set tableA_id = LAST_INSERT_ID();
But for multiple records it's wrong. Any idea how to update tableB after each insertion into tableA? Is it possible to do with MySql query, or just write some PHP script?
Try to get it done with trigger .
i am stuck in a simple update query.
i have a table say tabble1 containing 'name' and 'phone_no' column. Now when i upload csv file containing list of name and contact numbers, i want to update name of duplicate number with previous one. For ex. i have a row containing 'max' '8569589652'. now when i upload same number with another name say 'stela' '8569589652' then stela shuld get updated to max.
for this purpose i created another table say table2. then i collected all duplicate entries from table1 into table2. after that updated new entry with previous name.
following are my queries:
to collect all duplicate entries:
INSERT INTO table2 SELECT phone_no,name FROM table1
GROUP BY phone_no HAVING COUNT(*)>1;
to update duplicate entries in table1:
UPDATE table1.table2 SET table1.name=table2.name
WHERE table1.phone_no=table2.phone_no ;
My problem is when i run these two query it is taking tooo much of time. It is taking ore than half an hour to upload csv file of 1000 numbers.
Please suggest me optimize query to upload csv in less time.
does speed of uploading matters with size of database..
please help.
thanks in advance.
Here are the steps from the link I suggested.
1) Create a new temporary table.
CREATE TEMPORARY TABLE temporary_table LIKE target_table;
2) Optionally, drop all indices from the temporary table to speed things up.
SHOW INDEX FROM temporary_table;
DROP INDEX `PRIMARY` ON temporary_table;
DROP INDEX `some_other_index` ON temporary_table;
3) Load the CSV into the temporary table
LOAD DATA INFILE 'your_file.csv'
INTO TABLE temporary_table
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(field1, field2);
4) Copy the data using ON DUPLICATE KEY UPDATE
SHOW COLUMNS FROM target_table;
INSERT INTO target_table
SELECT * FROM temporary_table
ON DUPLICATE KEY UPDATE field1 = VALUES(field1), field2 = VALUES(field2);
5) Remove the temporary table
DROP TEMPORARY TABLE temporary_table;
You can update duplicate entries of "phone no" like below.
INSERT INTO table2 (phone_no,name)
VALUES
('11111', aaa),
('22222', bbb),
('33333', cccc),
ON DUPLICATE KEY UPDATE
phone_no = VALUES(phone_no);
Dump your CSV file in a temp table.
Then aply merge statement simply
Merge
AS MAIN
USING
AS TEMP
On
MAIN.CONTACT_NO=TEMP.CONTACT_NO
WHEN MATCHED THEN UPDATE
MAIN.NAME=TEMP.NAME;
IF YOU WANT TO INSERT NON MATCHING RECORD USE THIS
WHEN NOT MATCHED
THEN INSERT
(NAME,
CONTACT_NO)
VALUES
(
TEMP.NAME,
TEMP.CONTACT_NO
);
Please not that merge command must end with ';'
I have used ';' after upadate remove that and add the below part and end the whole merge with ';'
Hope this helps
Please update if any more help needed.
i have a contactnumber column in mysql database. In contactnumber column there are more than 20,000 entries. Now when i upload new numbers through .csv file, i dont want duplicate numbers in database.
How can i avoid duplicate numbers while inserting in database.
I initially implemented logic that checks each number in .csv file with each of the number in database.
this works but takes lot of time to upload .csv file containing 1000 numbers.
Pleae suggest how to minimize time required to upload .csv file while not uploading duplicate values.
Simply add a UNIQUE constraint to the contactnumber column:
ALTER TABLE `mytable` ADD UNIQUE (`contactnumber`);
From there you can use the IGNORE option to ignore the error you'd usually be shown when inserting a duplicate:
INSERT IGNORE INTO `mytable` VALUES ('0123456789');
Alternatively, you could use the ON DUPLICATE KEY UPDATE to do something with the dupe, as detailed in this question: MySQL - ignore insert error: duplicate entry
If your contactnumber should not be repeated then make it PRIMARY or at least a UNIQUE key. That way when a value is being inserted as a duplicate, insert will fail automatically and you won't have to check beforehand.
The way I would do it is to create a temporary table.
create table my_dateasyyyymmddhhiiss as select * from mytable where 1=0;
Do your inserts into that table.
and then query out the orphans on the between mytable and the temp table based on contactnumber
then run an inner join query between the two tables and fetch out the duplicate for your telecaller tracking.
finally drop the temporary table.
Thing that this does not address are duplicates within the supplied file (don't know if that would be an issue in this problem)
Hope this help
If you don't want to insert duplicate values in table and rather wants to keep that value in different table.
You can create trigger on table.
like this:
DELIMITER $$
CREATE TRIGGER unique_key BEFORE INSERT ON table1
FOR EACH ROW BEGIN
DECLARE c INT;
SELECT COUNT(*) INTO c FROM table1 WHERE itemid = NEW.itemid;
IF (c > 0) THEN
insert into table2 (column_name) values (NEW.itemid);
END IF;
END$$
DELIMITER ;
I would recommend this way
Alter the contactnumber column as UNIQUE KEY
Using phpmyadmin import the .csv file and check the option 'Do not abort on INSERT error' under Format-Specific Options before submitting
How to duplicate a table with keys & other structure features retained? including primary key, foreign keys, and indexes.
Can this be done with a single MySQL query?
I'm using "create table newtable as select..." but this method makes all keys & indexes lost.
duplicating a table from another table (with indexing and structure)cannot be done with a single query
you will need need 2 queries.
1) To create Duplicate table.
CREATE TABLE Table2 LIKE Table1;
This will create an exact copy of the table.
2) Fill in the Duplicate table with values from original table.
INSERT INTO Table2 SELECT * from Table1;
will fill Table2 with all the records fom Table1
you can do it in this query
CREATE TABLE a LIKE b
after you can insert
INSERT INTO a SELECT * FROM b
read more information in this article
Following query creates and duplicates data.
CREATE TABLE table2 SELECT * FROM table1