This question already has answers here:
Avoid duplicates in INSERT INTO SELECT query in SQL Server
(11 answers)
Closed 1 year ago.
In sql I have one blank table and second one with values
In me second one I have column with values like
poland-data
russia-data
usa-data
england-data
poland-data-hr
england-data-hr
england-hr
poland-hr
I want to copy to the blank table column with values without 'hr' from table nr.2
for example i only want to see in that table
poland-data
russia-data
usa-data
england-data
You can try following approach:
SELECT distinct REPLACE(col1 ,'-hr', '') as col1
INTO TableTwo
FROM TableOne
It's not entirely clear what you are asking. Is this a specific case of something that will have to be done many times with variable columns? If not, you should be able to just to
CREATE TABLE TABLE_2 AS
SELECT poland_data,
russia_data,
usa_data,
england_data
FROM TABLE_1;
Or
INSERT INTO TABLE_2
SELECT poland_data,
russia_data,
usa_data,
england_data
FROM TABLE_1;
If you already have the table created.
Related
This question already has answers here:
Get the new record primary key ID from MySQL insert query?
(13 answers)
Closed 3 years ago.
INSERT INTO db.a (a,b,c,d,e,f,g,h,i)
VALUES (2,2,"a",'b','c','d',1,'e',0)
The first insert will insert a new row with a incremental_id. I need to take that incremental_id and make an entry into db.b using that incremental_id, for example, if it was 6005 I would make the following insert.
INSERT INTO db.b
(a_id, s_id, use_i)
VALUES (6005,7,0)
How can I automatically grab the id of the first insert so my second insert can be built dynamically after the first query?
You can use function LAST_INSERT_ID() which will return exactly what you need.
This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 7 years ago.
I want to delete duplicate columns in mysql.
Here is sample:
person------city
Tom--------NY
Tom--------NY
Jhon-------LA
Desired output like this
person------city
Tom--------NY
Jhon-------LA
How can we do this in MySQL?
If you do not have primary key in your table, then it is easier to create new table with distinct values and then insert back into original table (assuming your table name is "ORIGINAL_TABLE" ) :
Create table Table2 as Select distinct person, city from ORIGINAL_TABLE;
truncate table ORIGINAL_TABLE;
insert into ORIGINAL_TABLE (person, city ) Select person, city from Table2
drop table Table2;
This question already has answers here:
In MySQL, can I copy one row to insert into the same table?
(26 answers)
Closed 7 years ago.
A great way to duplicate a row in MySQL is to use INSERT INTO ... SELECT FROM syntax.
For example:
INSERT INTO tblExample (col1, col2, col3)
SELECT col1, col2, col3 FROM tblExample WHERE pkey = 1234;
This is easy and straightforward, but from a code maintenance standpoint this is one more statement to keep track of if there are any schema changes. Suppose I add an additional column to the tblExample table, col4; now I have to remember to go back and update this SQL statement in my code. If I fail to do so, then I've just introduced a bug.
With this in mind, is there an easy to way to copy the whole row, whatever the schema may be, except for the primary key?
Very much against best practices you can do the following:
INSERT INTO myTable
SELECT * FROM myTable WHERE thisField = "abcd"
A somewhat inelegant way:
CREATE TEMPORARY TABLE tmpTable ENGINE=MEMORY SELECT * FROM realTable WHERE pk = 'something';
UPDATE tmpTable SET pk = 'something else' ;
INSERT INTO realTable SELECT * FROM tmpTable;
This question already has answers here:
With MySQL, how can I generate a column containing the record index in a table?
(8 answers)
Closed 8 years ago.
I have: Select some set of pairs. The first column is the id of row in table, the second is the new value which should be assigned to that row.
-- the first query
CREATE TABLE tmp;
SELECT row_id, new_value
FROM [not essential tables and joins]; //PSEUDOCODE
-- another queries
FOR EACH tmp //PSEUDOCODE
UPDATE table SET value = new_value WHERE id = row_id;
-- QUESTION: CAN I MERGE SELECT AND UPDATE IN ONE QUERY?
-- I want avoid creating temporary table.
Problem: Iteration through table (as in example above) decrease clearness and speed of code.
Question: *How to do the same in single query
I think you are looking for update the table join with other table (Not sure though). You can do something like
UPDATE tmp a
JOIN sometable b ON a.col = b.col
AND a.id = b.row_id
SET a.value = b.new_value
I have two data base A & B, in each one I have a table called answer, I want to use the 2nd one as an archive table, I want to create a trigger that copies the last inserted row in A.answer to B.answer.
Here what I did
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW INSERT INTO `B`.`answer` SELECT * FROM `answer`
This trigger works, but copy all the answers inserted in A.answer to B.answer.
The problem is : I dont want to copy all answers, but only the last one.
(remark : I dont know the id of the inserted answer, so dont tell me to add a ' WHERE answer.id = xx ').
Thanks for your help
You could write your trigger this way:
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW
INSERT INTO `B`.`answer` VALUES (NEW.col1, NEW.col2, ..., NEW.colN)
where you have to specify all column names.
Please see fiddle here.