Remove duplicates condition to column value - mysql

I have two tables - Table_1 and Table_2.
They have identical columns - Col1, Col2, Col3, Col4(integer).
Col4 value for all Table_1 rows is 1.
Col4 value for all Table_2 rows is 2.
I insert Table_2 rows into Table_1.
Table_1 and Table_2 have some duplicate values based on Col1 and Col2. I need to retain the Table_2 values and delete the Table_1 duplicate values.
For example:
Col1,Col2,Col3,Col4
1) a ,b ,c ,1 (From Table_1)
2) a ,b ,d ,2 (From Table_2)
I tried the following -
ALTER TABLE Table_1 ADD UNIQUE (Col1, Col2);
It removed the Table_2 values instead of Table_1 values.
How do I solve this problem?

Put a unique key on col1, col2 and use on duplicate key update col3=VALUES(col3), col4=VALUES(col4) when you insert.

Another alternative. Create the unique key on col1, col2 and use the REPLACE INTO syntax.
REPLACE INTO Table_1 (col1, col2, col3, col4)
SELECT col1, col2, col3, col4 FROM Table_2
If a duplicate record exists, it will delete the record in Table_1 and insert the record from Table_2. If the record doesn't exist, it simply inserts it.

Related

How to copy one table into another with some additional Data

I want to copy columns from table1 into table2 with some additonal columns in table2.
Although I know the syntax:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
But how can I add an additional column's data into table2?
I've tried this approach but it gives syntax error:
// first storing the desired data from table1 into a temporary table
CREATE TEMPORARY TABLE temp_table
select column1, column2, column3 from table1
where condition;
// then placing the selected columns into table2
INSERT INTO table2 (col1, col2, col3,col4) values (
SELECT column1 FROM temp_table,
SELECT column2 FROM temp_table,
SELECT column3 FROM temp_table,
'Additional Value'
);
You can add a litteral string (or any other expression) to the column list. Consider:
INSERT INTO table2 (col1, col2, col3,col4)
SELECT
column1,
column2,
column3,
'Additional Value'
FROM table1
Please, follow below query and you can use alias as extra column it's
static or expression field:
(SQL-Fiddle)
INSERT INTO table2 (col1, col2, col3, col4)
SELECT column1, column2, column3, 'Additional Value' as column4
FROM table1
WHERE condition;

Merge several rows to own row by value of one column in MySQL

i have a table like this one:
There are some rows with the same value in col2 and e.g. for every 't1' is only one value in col3 (and so on), no duplicates.
and I want to have a query for this result:
with
SELECT * FROM table GROUP BY col2
i get no all values for t1 e.g..
You can pick min/max value per group as
SELECT
max(col1) col1,
col2,
max(col3) col3,
max(col4) col4,
max(col5) col5,
max(col6) col6,
max(col7) col7
FROM table
GROUP BY col2

MYSQL: Insert one value from another table while populating other columns regularly

Lets say we have a table (1):
id | col1 | col2
And another table (2):
id | col3
Task is to insert all col3 distinct values to col1 at the same time populating col2 with random integer value
A couple of solutions here.
This uses a sub query to return the distinct values of col2.
INSERT INTO table1 (id, col1, col2)
SELECT NULL, col2, FLOOR(RAND()*(1000))+1
FROM
(
SELECT DISTINCT col2
FROM table2
)
The following abuses the GROUP BY clause to only generate rows for distinct values of col2. While this should be OK on a default install of MySQL, it might not work depending on the options set up for your installation and also probably wouldn't work in other flavours of SQL.
INSERT INTO table1 (id, col1, col2)
SELECT NULL, col2, FLOOR(RAND()*(1000))+1
FROM table2
GROUP BY col2

Add "on duplicate key update" clause to insert into query

I'm starting with a query like this:
insert into summary ( col1, col2, Total )
select col1, col2, count(col4) as total from importdata
where col1 = 'abc' and col4 in ('1A', '2A')
group by col1, col2
order by col1, col2
and I haven't been able to determine how the correct 'on duplicate' clause. The clause I think I need is
on duplicate key update total=count(col4)
and I've placed it as the very last line in the query and as the line after the where clause, but both generated errors. Is my clause even correct and where does it need to go?
(Worst case I can use 'insert ignore', but I think doing the update would be better.)
You can't use COUNT or other group functions in the ON DUPLICATE KEY UPDATE clause. What you can do instead is this:
INSERT INTO summary ( col1, col2, Total )
SELECT col1, col2, count(col4)
FROM importdata
WHERE col1 = 'abc' AND col4 IN ('1A', '2A')
GROUP BY col1, col2
ORDER BY col1, col2
ON DUPLICATE KEY UPDATE Total = VALUES(Total)
This says, if there is a duplicate key, instead of inserting a new row just set the column total to the value you would have inserted in Total. Note that I got rid of the as total -- that would have caused problems as you already have a column named Total, and the names are case-insensitive.
You cannot use functions for the duplicate key update, however, you could create a variable and then use that variable.
INSERT INTO summary (col1, col2, Total)
select col1, col2, #totalCount := count(col4) as Total from importdata
where col1 = 'abc' and col4 in ('1A', '2A')
group by col1, col2
order by col1, col2
) ON DUPLICATE KEY UPDATE Total = #totalCount;

ON DUPLICATE KEY UPDATE - help needed

My problem is i have an insert that updates the query on duplicate key and it is like the one below:
INSERT INTO TABLE
(COL1, COL2, COL3 , ETC...)
SELECT
COLA1, COLA2, COUNT(1) , ETC...
FROM TABLE2
WHERE 'CONDITION'
GROUP BY COL1, COL2, COL3
ON DUPLICATE KEY UPDATE COL1=VALUES(COLA1), COL3=COUNT(1)
THIS QUERY RETURNS AN ERROR: General error: 1111 Invalid use of group function SQL
COL1, COD2, COL3 ARE COMPLEX KEY.
Try this:
INSERT INTO TABLE(COL1, COL2, COL3, ETC...)
SELECT COLA1, COLA2, COUNT(1), ETC...
FROM TABLE2
WHERE 'CONDITION'
GROUP BY COL1, COL2, COL3
ON DUPLICATE KEY UPDATE COL1 = VALUES(COL1), COL3 = VALUES(COL3);
That is, refer to the names in the values1 statement, not the expressions in the select statement.