1, create a table
CREATE TABLE `user` ( `id` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `address` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2, the data inserted into the corresponding
INSERT INTO `user` VALUES ('1', 'zhangsan', '20', '上海'); INSERT INTO `user` VALUES ('2', 'zhangsan', '20', '北京'); INSERT INTO `user` VALUES ('3', 'lisi', '25', '北京'); INSERT INTO `user` VALUES ('4', 'lisi', '25', '深圳'); INSERT INTO `user` VALUES ('5', 'wangwu', '30', ' Shenzhen ' ); INSERT INTO ` the User ` VALUES ( ' 6 ' , ' wangwu ' , ' 30 ' , ' Shanghai ' ); INSERT INTO ` the User ` VALUES ( ' 7 ' , ' zhaoliu ' , ' 35 ' , ' Guangzhou ' );
3, common operations
# Copy a table the CREATE TABLE ` the User ` AS the SELECT * the FROM `user_temp` # create an empty table the CREATE TABLE user_temp1 like ` the User ` # delete a table DROP TABLE ` the User `
4, aid count keyword
DELETE FROM `user` WHERE id in( SELECT v.minid FROM( SELECT COUNT(*) num,`name`,age,MAX(id) minid FROM `user` GROUP BY `name`,age ) v WHERE v.num>1 )
5, the use of having keywords
DELETE FROM `user` WHERE id in( SELECT id FROM( SELECT MAX(id) id,`name`,age,address FROM `user` t GROUP BY t.`name` HAVING COUNT(`name`)>1 ) v )
Note: delete statement delete a query result set is not going to work, have to rely on word query is embedded view to complete the deletion function