CREATE TABLE logs_cnt SELECT term,COUNT(term) AS count_term FROM logs GROUP BY term; ALTER TABLE logs_cnt ADD PRIMARY KEY(term); update logs a Inner join logs_cnt b using (term) set a.count=count_term; DROP TABLE logs_cnt;