CREATE TABLE logs_cnt SELECT *, 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) ON a.count = b.count_term; DROP TABLE logs_cnt;