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

Viewed 1825

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?

3 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.

You can use

insert into table[b](query,date)
select query,date from table[a] order by table[a] asc
INSERT INTO tableB
SELECT * FROM tableA
group by query

Note: please remove id from both the tables while applying the above query.

Related