创建数据库
CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
查看数据库
mysql> show databases;
创建表
DROP TABLE IF EXISTS tbkt.msg_market;
create table IF NOT EXISTS tbkt.msg_market (
id int(11) unsigned not null auto_increment,
type int(1) not null unique,
user_id int(11) not null,
phone_number varchar(11) not null,
content varchar(1000) not null default "",
add_time datetime not null,
primary key(id)
) engine=innodb default charset=utf8 auto_increment=1;
添加字段
mysql> alter table knowledge_question add COLUMN multiple int(1) not NULL DEFAULT 0;
Query OK, 0 rows affected (1.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改字段
mysql> alter table knowledge_question modify column json_answer text not NULL;
Query OK, 2973 rows affected (1.42 sec)
Records: 2973 Duplicates: 0 Warnings: 0
mysql> alter table knowledge_question change area display int(1) not null default 0;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
#modify适用于只改变字段类型,change可以改变类型也可以改变列名称,但使用change如果在不改变名称的情况下也需要输入新的字段名称。
删除字段
mysql> alter table knowledge_question drop column answer;
Query OK, 0 rows affected (0.62 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加索引(普通索引)
mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` ); # 索引名字定义表名加字段名加“_index”标记
删除索引
mysql> drop index task_new_study_detail_1f92e550 on task_new_study_detail;
[SQL] drop index task_new_study_detail_1f92e550 on task_new_study_detail;
受影响的行: 0
时间: 0.098s
查看表结构
desc 表名;
show columns from 表名;
show create table 表名;
删除表
DROP TABLE IF EXISTS `table_name`;
删除数据库
drop database database_name;
更新表数据
update mmsc_word set send_status=2 where send_status=0;
update zy_book set `cover`=replace(`cover`, ' ', ''); -- 更新空格为空字符串
select * from zy_book t where replace(t.cover, ' ', 'a')="a" -- 查询为空格的数据
同时操作多个表
update zy_video t, zy_question_ask t2 set t2.video_id=t.id where t.object_id=t2.id and t.type=3;
根据条件删除表数据
delete from mmsc_word where send_status=2;
连接查询后删除数据
delete ap from account_profile ap join auth_user au on ap.user_id=au.id where !(INSTR(au.username,"xs")>0 or INSTR(au.username,"js")>0 or INSTR(au.username,"jz")>0) and au.username!="dt001";
插入数据
INSERT INTO 目标表 SELECT * FROM 来源表; -- 插入全部字段
INSERT INTO newArticles SELECT * FROM articles; -- 插入全部字段
INSERT INTO 目标表 (字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM 来源表; -- 插入指定字段
INSERT INTO tb_name (col1,col2) VALUES(value1,value2);
INSERT INTO users(name, age) VALUES('姚明', 25), ('比尔.盖茨', 50), ('火星人', 600); # 一次插入多条记录
create table yy_role_image select * from tbkt.english_role_image; -- 创建表并插入数据
清空表数据
truncate table 表名;
查询语句
正则查询
select * from ziyuan_new.yy_standard_pool_detail t where t.text REGEXP '[^|.|, ]*(dance[.|, ]+)'; // 查询包含"dance"单词的句子。
select * from ziyuan_new.yy_standard_pool_detail t where t.text REGEXP BINARY '[^|.|, ]*(dance[.|, ]+)'; // 区分大小写加上binary关键字。
查询结果添加序号
set @i = 0;
select @i := @i + 1 as `order`, id from sx2_question t where t.`subject` like "%data:image%";
单词更新首字母大写
update yy2_word set lesson=concat(UPPER(left(lesson,1)),substring(lesson,2,(length(lesson)-1))) where lesson<>"";
分组过程中字符串相加
select q.id, GROUP_CONCAT(a.id) from question q join question_ask a on q.id=ask.question_id group by q.id; -- GROUP_CONCAT默认已","分隔。
自定义排序
select * from auth_user order by field(roleId,2,3,4,1,5); select * from auth_user order by locate(id,'2,3,1,5,4');
例如:select * from sx2_catalog_relate_knowledge where catalog_id in (83, 82, 84) order by field(catalog_id, '83, 82, 84');