3

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?

etarion
  • 16,935
  • 4
  • 43
  • 66

3 Answers3

7

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.

  • @Shafiullah - please post the full error you got. The query I posted shouldn't give a syntax error it is correct. –  Sep 22 '17 at 12:48
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unique(query,date) select query, max(date) As maxdate from 500k group by query' at line 1 – Shafi ullah Sep 22 '17 at 13:08
  • @Shafiullah - There is no `unique(query,date)` in my query. It is something you added by yourself. Can you please update your question with the full query you are trying to execute? –  Sep 22 '17 at 13:09
0

You can use

insert into table[b](query,date)
select query,date from table[a] order by table[a] asc
Alejandro
  • 7,290
  • 4
  • 34
  • 59
0
INSERT INTO tableB
SELECT * FROM tableA
group by query

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

adiga
  • 34,372
  • 9
  • 61
  • 83
Solomon Suraj
  • 1,162
  • 8
  • 8