mysql


创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE `dmp_tag_collection` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`tag_id` int(11) NOT NULL,
`tag_name` varchar(255) NOT NULL DEFAULT '',
`tag_description` varchar(255) NOT NULL,
`tag_operator` varchar(255) NOT NULL,
`tag_value` varchar(255) NOT NULL,
`data_url` varchar(255) NOT NULL,
`data_count` int(11) NOT NULL,
`data_updated_at` datetime(6) NOT NULL,
`created_at` datetime(6) DEFAULT CURRENT_TIMESTAMP(6),
`updated_at` datetime(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`deleted_at` datetime(6) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
1
2
3
4
5
6
7
8
create table department(
id int(10) primary key not null auto_increment,
name VARCHAR(20) not null,
create_user varchar(20) not null,
create_time datetime DEFAULT CURRENT_TIMESTAMP,
modify_user varchar(20) not null,
modify_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

增加列

1
2
3
4
5
6
7
8
9
10
11
12
13
alter table weekly_release add column modify_time timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间';

-- 在表的最后一列增加新的一列
ALTER TABLE `tbname`
ADD COLUMN `state` TINYINT(2) NOT NULL DEFAULT '0' COMMENT '0为添加1为编辑'

-- 在指定的位置增加新的一列
ALTER TABLE `tbname`
ADD COLUMN `state` TINYINT(2) NOT NULL DEFAULT '0' COMMENT '0为添加1为编辑' AFTER `column_name`;

-- 在第一列增加新的一列
ALTER TABLE `tbname`
ADD COLUMN `state` TINYINT(2) NOT NULL DEFAULT '0' COMMENT '0为添加1为编辑' FIRST;

从一个库到另一个库

1
CREATE TABLE weekly_release LIKE data_analyze.weekly_release;

日期转换

1
date_format(FROM_UNIXTIME(first_opentime/1000),'%Y%m%d')

在有数据时更新,没有数据时插入

1
2
3
REPLACE into table (id, name, age) values(1, "A", 19)

INSERT INTO table (id,name,age) VALUES('1','Mohammad','21') ON DUPLICATE KEY UPDATE name='Mohammad',age='21'

删除

1
2
3
4
DELETE FROM 
aaa_app_publishers
WHERE
country=''

插入

1
2
3
4
INSERT INTO
table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );

删除重复的数据并保留一行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DELETE FROM
aaa_app_publishers_old1
WHERE
publisher_id IN (
SELECT
publisher_id
FROM
(SELECT
publisher_id, COUNT( * )
FROM
aaa_app_publishers_old1
GROUP BY
publisher_id HAVING COUNT( * ) > 1 )
AS a
)
LIMIT 1;

  mysql
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×