常用sql整理(更新中...)


插入或者更新数据,如果没有记录则插入记录,如果存在则更新记录[仅限mysql]

#新建表
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
#插入记录
INSERT INTO `db0`.`test` (`id`, `name`) VALUES ('10', 'to6');

#插入或者更新记录
insert into test (id, name) VALUES(10,'jetty' ) ON DUPLICATE KEY UPDATE name = VALUES(name);

添加字段

alter TABLE app add citycode varchar(6) not null default 0; # 城市代码

修改字段类型

alter TABLE app modify column citycode VARCHAR(30);

删除字段
 

ALTER TABLE appDROP COLUMN citycode;

新增索引
 

alter table app add constraint unique_app_appid unique (appid);

修改索引类型
#删除索引

ALTER TABLE table1 DROP INDEX unique_table1_consum1;

#创建索引

CREATE INDEX index_table1_consum1 ON table1(consum1);

通过select语句把一列数据拼接成一条字符串

select GROUP_CONCAT(a.`name`) from ads a;

发布了48 篇原创文章 · 获赞 7 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/maguoliang110/article/details/85250022