最新消息:这里是最新消息

mysql合并多张结构相同的数据表

php+mysql benty 737浏览 0评论

数据去重:

DELETE FROM xx
WHERE other in
    (
    SELECT other FROM
                      (
                      SELECT other FROM xx GROUP BY other HAVING count(other)>1
                      ) t1
    )
AND id NOT IN
    (
    SELECT id FROM
                    (
                    SELECT min(id) id FROM xx GROUP BY other HAVING count(other)>1
                    ) t2
    )
————
执行多个数据表的合并
CREATE TABLE goods_xinxi
select from goods_xinxi_0
UNION ALL
select from goods_xinxi_1
UNION ALL
select from goods_xinxi_2
UNION ALL
select from goods_xinxi_3
UNION ALL
select from goods_xinxi_4
UNION ALL
select from goods_xinxi_5
————–
对主键重新进行排序

1、备份表结构

create table table_bak like table_name;

2、备份表数据

insert into table_bak select * from table_name;

3、删除原来主键字段(如id)

alter table table_name drop id;

4、添加主键,自增,放在第一位

alter table table_name add id int(11) primary key auto_increment first;

搞定

5、检查没问题的话,备份的表可以删了

drop table table_name;


查询某个字段的最大字节

首先查询字段的最大长度值

SELECT max(length(message)) FROM apple;

假设查询结果为60220
接着查询具体的记录

SELECT * FROM apple where length(message)=60220;


mysql修改字段类型

mysql 修改字段长度

alter table news  modify column title varchar(130);

alter table 表名 modify column 字段名 类型;

如:news 表里的title  字段 原来长度是 100个字符,现长度要改成130个字符

alter table news modify column title varchar(130);


创建索引

其中idinfo是字段,userinfo是数据表.
查看当前数据表的索引:
show index from userinfo;
删除重复的索引:
drop index IDinfo_2 on userinfo;
添加创建字段索引
alter table userinfo add index(Idinfo);

查看一个语句是否使用索引,可用explain关键字进行查看.

explain select * from userinfo where id=”4544545″;

转载请注明:稻香的博客 » mysql合并多张结构相同的数据表

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址