新增
1 2 3 4 5 6 |
#新增数据 insert into `表名` (`字段1`,`字段2`,`字段3`) values('值1','值2','值3'); #把源数据表的记录添加到目标数据表 insert into `目标数据表` select * from `源数据表`; |
查询
1 2 3 |
#按字段排序 顺序=ASC 倒序=DESC SELECT * FROM `表名` ORDER BY `排序字段` DESC; |
修改
1 2 3 |
#批量替换指定字段部分内容 update `表名` set `字段` = replace(`字段`,'被替换的文字','替换文字') where `条件字段` like '%被替换的文字%'; |
删除
1 2 3 4 5 6 7 8 9 |
#条件表达式删除 delete from `表名` where `条件字段` = '值'; #删除字段值包含某内容的记录 delete from `表名` where `条件字段` like '%内容%'; #将数据表所有记录删除 delete from `表名`; |
MySQL表达式函数
- AVG(字段名)得出一个表格栏平均值
- COUNT(*|字段名)对数据行数的统计或对某一栏有值的数据行数统计
- MAX(字段名)取得一个表格栏最大的值
- MIN(字段名)取得一个表格栏最小的值
- SUM(字段名)把数据栏的值相加
1 2 3 |
#sum函数用法 seect sum(`字段名`) as `别名` from `表名` where `字段` = '值' |
存储过程操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#删除存储过程 DROP PROCEDURE if EXISTS init_fg_data; #创建存储过程 CREATE PROCEDURE init_fg_data() BEGIN declare id int default 2200000; declare num int default 4000000; while num > 0 DO INSERT INTO `cms_content`(`id`, `site_id`, `title`, `user_id`, `check_user_id`, `category_id`, `model_id`, `parent_id`, `quote_content_id`, `copied`, `contribute`, `author`, `editor`, `only_url`, `has_images`, `has_files`, `has_products`, `has_static`, `url`, `description`, `tag_ids`, `dictionary_values`, `cover`, `childs`, `scores`, `comments`, `clicks`, `publish_date`, `expiry_date`, `check_date`, `update_date`, `create_date`, `sort`, `status`, `disabled`) VALUES (id, 1, CONCAT(id,'重庆市人民政府办公厅发重庆市人民政府办公厅转发市建委关于贯彻<<中华人民共和国招标法>>严格建设工程招标投标监督管理意见>>的通知'), 1, 1, 3, 'fg', NULL, NULL, 0, 0, '重庆市人民政府办公厅', '渝办发〔2000〕37号', 0, 0, 0, 0, 0, concat('content/checkProjectDetail.html?id=',id), 'http://www.cebpubservice.com/ctpsp_policylaw/jsp/zz/policies_info.jsp?documentId=daafd2820cf24f76812f9512b244b447', NULL, 'wjlx_6 wjzt_2 dqfw_50', NULL, 0, 1, 1, 2, now(), NULL, NULL, NULL, now(), 0, 1, 0); INSERT INTO `cms_content_attribute`(`content_id`, `source`, `source_url`, `data`, `search_text`, `text`, `word_count`) VALUES (id, NULL, NULL, NULL, '搜索内容', '内容', 800); set id = id + 1; SET num = num - 1; END WHILE; END #执行存储过程 CALL init_fg_data(); |
转载请注明:追风逐雨 » 常用MySQL语句汇总