Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy only distinct values from one table to another in Mysql?

I have a MySql database round-about 2.5GB,

The table[A] has following columns, |anoid| |query| |date| |item-rank| |url|

I have just created another table[b] having columns only |query| and |date|

I want to insert all the distinct records in query column, with it's respective date, from Table[A] to [B], is there any fast query?

like image 567
Shafi ullah Avatar asked Oct 16 '25 15:10

Shafi ullah


1 Answers

Use INSERT INTO ... SELECT:

INSERT INTO Tableb(query, date)
SELECT query, MAX(Date) AS MAXDate
FROM Tablea
GROUP BY query

This will give you distinct query with the most recent date.