码迷,mamicode.com
首页 > 编程语言 > 详细

常用的数据语言

时间:2019-12-24 12:10:47      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:not   user   crt   null   tin   alt   after   年龄   har   

/*创建数据库*/
CREATE DATABASE z_testOcean

/*删除数据库*/
DROP DATABASE z_testOcean

/*创建新表*/
CREATE TABLE z_table_1
(
`Id` INT(10) NOT NULL PRIMARY KEY,
`FirstName` VARCHAR(255) NOT NULL ,
`CrTime` DATETIME NOT NULL
)
DROP TABLE `z_testocean`.`z_table_1`

/*comment:评论,意见*/
CREATE TABLE `z_testocean`.`z_staff_info`
(
`Id` INT(10) NOT NULL PRIMARY KEY COMMENT‘#主键‘,
`Name` VARCHAR(128) NOT NULL COMMENT‘姓名‘ ,
`Sex` VARCHAR(16) NOT NULL COMMENT‘性别‘,
`Age` VARCHAR(16) NOT NULL COMMENT‘年龄‘,
`Salaly` INT(10) NOT NULL COMMENT ‘工资‘
)

/*插入单条数据*/
INSERT INTO z_staff_info(`Id`, `Name`, `Sex`, `Age`, `Salaly`) VALUES(1001,‘小张‘,‘男‘,‘35‘,6500)
/*插入多条数据*/
INSERT INTO z_staff_info(`Id`, `Name`, `Sex`, `Age`, `Salaly`) VALUES(1005,‘小张‘,‘男‘,‘35‘,6500),(1002,‘小li‘,‘男‘,‘45‘,8500),(1003,‘小hong‘,‘女‘,‘35‘,4500),(1004,‘小张‘,‘女‘,‘35‘,6800)

INSERT INTO z_staff_info(`Id`, `Name`, `Sex`, `Age`, `Salaly`) VALUES(1006,‘徐‘,‘男‘,‘23‘,6500)

/*更改数据*/
UPDATE z_staff_info SET `Name`=‘小徐‘ ,`Salaly`=6400 WHERE `Id`=1005

/*查询数据*/
SELECT *FROM z_staff_info
SELECT DISTINCT `Age` FROM z_staff_info
SELECT * FROM z_staff_info WHERE Salaly > 3000
SELECT * FROM z_staff_info WHERE Salaly > 3000 AND Age = ‘男‘
SELECT * FROM z_staff_info WHERE Salaly > 3000 AND Age = ‘男‘ ORDER BY Id DESC --降序排序
SELECT * FROM z_staff_info WHERE Salaly > 3000 AND Age = ‘女‘ ORDER BY Id ASC --升序排序
SELECT * FROM z_staff_info WHERE Salaly > 3000 AND `Name` LIKE ‘%hong%‘

/*删除某条数据*/
DELETE FROM z_staff_info WHERE `Name`=‘徐‘

/*新建一列*/
ALTER TABLE `z_testocean`.`z_staff_info` ADD COLUMN `Post` VARCHAR(128) NULL COMMENT‘职称‘ AFTER `Salaly`

/*将列Id的设置为自增,AUTO_INCREMENT*/
ALTER TABLE `z_testocean`.`z_table_2` CHANGE `Id` `Id` INT(10) NOT NULL AUTO_INCREMENT;

UPDATE z_staff_info SET `Post`=‘工程师‘ WHERE `Name`=‘小li‘;
UPDATE z_staff_info SET `Post`=‘会计‘ WHERE `Name`=‘小hong‘

/*不常用查询*/
SELECT * FROM z_staff_info WHERE `Post` IS NULL
SELECT * FROM z_staff_info WHERE `Post` IS NOT NULL
SELECT * FROM z_staff_info WHERE Age<>"女"

SELECT MAX(`Salaly`) FROM z_staff_info;
SELECT MIN(`Salaly`) FROM z_staff_info;
SELECT * FROM z_staff_info WHERE `Salaly` > ( SELECT AVG(`Salaly`) FROM z_staff_info )
SELECT COUNT(`Salaly`) FROM z_staff_info WHERE `Salaly` > ( SELECT AVG(`Salaly`) FROM z_staff_info )

ALTER TABLE `z_testocean`.`z_staff_info`
ADD COLUMN `Post` VARCHAR(128) NULL COMMENT ‘职称‘ AFTER `Salaly`;

/*删除多张表,表之间用,分割*/
DROP TABLE `z_table_test1` ,`z_table_test`

/*查询(information_schema数据库里面的)tables表中所有的自增ID:*/
/*多个数据库下所有有自增的*/
SELECT AUTO_INCREMENT FROM information_schema.`TABLES`;

/*查询指定表自增的Id*/
SELECT AUTO_INCREMENT FROM information_schema.`TABLES` WHERE table_name=‘z_table_2‘;

/*查询指定数据库,指定表名下的自增Id*/
SELECT AUTO_INCREMENT FROM information_schema.`TABLES` WHERE table_schema=‘z_testocean‘ AND table_name=‘z_table_2‘;


/*得到SQL语句删除表*/
SELECT CONCAT( ‘drop table ‘, table_name, ‘;‘ ) FROM information_schema.tables WHERE table_schema=‘z_testocean‘ AND table_name LIKE ‘%table%‘;
CONCAT( ‘drop table ‘, table_name, ‘;‘ )
DROP TABLE z_table_test1;
DROP TABLE z_table_test2;

/*SQL删除多个表的数据*/
/*删除一个从两个数据表。不能在一个多表DELETE语句中使用ORDER BY或LIMIT*/
DELETE `z_staff_info` FROM `z_staff_info`, `z_table_test1` WHERE z_staff_info.Id=z_table_test1.Id ;
/*删除两个三个数据表。*/
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;

DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
DELETE a, b FROM a , b WHERE (a.id = b.cat_id) AND a.id=?;

/*使用join删除两个数据表中的数据,LEFT要删除的数据库,如下:*/
DELETE a,b FROM a LEFT JOIN b ON a.id = b.cat_id WHERE a.id=6;
DELETE `z_staff_info`,`z_table_test1` FROM z_staff_info LEFT JOIN z_table_test1 ON z_table_test1.`FirstName`=z_staff_info.`Name` WHERE z_staff_info.`Name`=‘小hong‘;

/*遍历a张表,若有a表的名字在b表中有则删除b表数据*/
DELETE `z_staff_info`,`z_table_test1` FROM z_staff_info LEFT JOIN z_table_test1 ON z_table_test1.`FirstName`=z_staff_info.`Name` WHERE (SELECT )
/*当你引用表名时,必须使用引用名如:*/
DELETE `z_staff_info`,`z_table_test1` FROM category `z_staff_info left` JOIN article `z_table_test1` ON `z_staff_info`.`Name` = z_table_test1.`FirstName` WHERE z_staff_info.`Name`=‘小hong‘

/*表T1中有个t2_id字段,想将关联表T2中对应的字段内容转移到t1中来*/
UPDATE t1 LEFT JOIN t2 ON t1.t2_id = t2.id SET t1.name=t2.name,t1.phone=t2.phone WHERE t1.t2_id>0;

/*根据给定的一些无规则id批量修改数据*/
UPDATE Table_name SET Age=0 WHERE id IN(1000,1003);

#每分钟内的平均值
SELECT DATE_FORMAT(FROM_UNIXTIME(`time`),‘%Y-%m-%d %H:%i‘) AS TIME,AVG(tx_kb) FROM traffic WHERE machine_id=1234 GROUP BY DATE_FORMAT(FROM_UNIXTIME(`time`),‘%Y-%m-%d %H:%i‘);
#每小时内的平均值
SELECT DATE_FORMAT(FROM_UNIXTIME(`time`),‘%Y-%m-%d %H‘) AS TIME,AVG(tx_kb) FROM traffic WHERE machine_id=1234 GROUP BY DATE_FORMAT(FROM_UNIXTIME(`time`),‘%Y-%m-%d %H‘);

#一对多关系分组统计
SELECT `z_staff_info`.`Id`,`z_staff_info`.`Name`,COUNT(`z_table_2`.`Id`) AS user_num FROM z_staff_info,z_table_2 WHERE z_staff_info.id=z_table_2.id GROUP BY z_staff_info.id;

#按日期分类统计用户
SELECT FROM_UNIXTIME(`date`,‘%Y-%m-%d‘) days,COUNT(id) num FROM `z_staff_info` GROUP BY `date` ORDER BY `date` DESC;

#迁移表字段数据
INSERT INTO user2(`u_id`,`name`) SELECT `id`,`name` FROM USER;
INSERT INTO z_table_test2(Id,class, FirstName,Gread,crTime) SELECT * FROM `z_table_test1`

#复制一张表

#仅仅表结构
CREATE TABLE 数据表名 LIKE 源数据表}
CREATE TABLE z_staff_info_copy LIKE z_staff_info;
#仅仅表结构和数据
CREATE TABLE 数据表名 AS SELECT * FROM 源数据表名;
CREATE TABLE z_staff_info_copy1 AS SELECT * FROM z_staff_info;

常用的数据语言

标签:not   user   crt   null   tin   alt   after   年龄   har   

原文地址:https://www.cnblogs.com/oceanran/p/12090286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!