数据库常用语法

刘德胜 4255次浏览

摘要:mysql数据库常用语法://一般查询 SELECT*FROM`table`WHEREid=1orderbyiddesc //多字段排序查询 SELECT*FROM`table`orderbynamedesc,addtimdesc,lasttiamasc //...

mysql数据库常用语法:

//一般查询
SELECT * FROM `table` WHERE id=1 order by id desc
//多字段排序查询
SELECT * FROM `table` order by name desc,addtim desc,lasttiam asc
//随机查询
SELECT * FROM `table` where id=1  ORDER BY RAND()  LIMIT 10
//统计条数
SELECT count(*) FROM `table`
//求和
SELECT sum(*) FROM `table`
//分类查询
SELECT DISTINCT `name` FROM `table`
//修改
UPDATE `table` SET `name`='张三' WHERE id=2
//取绝对值,1变0,0变1
UPDATE `table` status=ABS(status-1);
//插入数据
INSERT INTO `table`(`name`,`sex`) VALUES ('张三','男')
//删除数氢
DELETE FROM `table` WHERE id=1
//清空表
TRUNCATE `table`
//重新创建自动编号
ALTER TABLE `table`  DROP `id`
ALTER TABLE `table` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`)