mysql——对数据的操作

1:插入数据

INSERT INTO table_name ( field1, field2,...fieldN )
                       VALUES
                       ( value1, value2,...valueN );

如果数据是字符型,必须使用单引号或者双引号,如:"value"

2:排序

SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]

3:分组

SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

4:处理重复数据

1>预防

设置指定的字段为 PRIMARY KEY(主键) 或者 UNIQUE(唯一) 索引来保证数据的唯一性。

2>统计

mysql> SELECT COUNT(*) as repetitions, filed1, filed2 
    -> FROM tablename
    -> GROUP BY filed1, filed2
    -> HAVING repetitions > 1;

3>过滤

使用distinct:SELECT DISTINCT filed1, filed2 -> FROM tablename;
使用group by:SELECT filed1, filed2 FROM tablename GROUP BY (filed1, filed2);

4>删除

利用索引和主键:

 ALTER IGNORE TABLE tablename
    -> ADD PRIMARY KEY (filed1, filed2);

你可能感兴趣的:(数据库)