根据字段名查询所有包含的表:SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = '字段名'
查看分区
SELECT * FROM INFORMATION_SCHEMA.PARTITIONS WHERE table_name ='c5_call_sheet_query'
增加分区
SELECT * FROM INFORMATION_SCHEMA.PARTITIONS WHERE table_name ='c5_call_sheet_query'
增加分区
ALTER TABLE `c5_call_record`.c5_call_sheet_query ADD PARTITION (PARTITION p201401 VALUES LESS THAN ('2014-02-01') ENGINE = INNODB);
重启MySQL:/etc/init.d/mysqld restart
启动MySQL:/data/env/mysql/bin/mysqld_safe --user=mysql &
MySQL的配置文件一般在/etc/下
零碎知识点:
获取本月最后一天:last_day(curdate())
天数加减以及日期格式化:convert(date_format((now() + interval 7 day),'%Y-%m-%d') using utf8)
(一) 查看当前是否已开启事件计划(调度器)有3种方法:
1) SHOW VARIABLES LIKE 'event_scheduler';
2) SELECT @@event_scheduler;
3) SHOW PROCESSLIST;
(二) 开启事件计划(调度器)开关有4种方法:
1) SET GLOBAL event_scheduler = 1;
2) SET @@global.event_scheduler = 1;
3) SET GLOBAL event_scheduler = ON;
4) SET @@global.event_scheduler = ON;
键值1或者ON表示开启;0或者OFF表示关闭;
如果出现上面的错误,执行下面的SQL就可以给bfsql@%赋予创建Event的权限:
UPDATE mysql.user SET Event_priv = 'Y' WHERE HOST='%' AND USER='bfsql';
如果你这个时候再次执行创建Event的SQL,还是会出现上面的错误,因为你需要执行:
FLUSH PRIVILEGES;最后,你可以通过SHOW GRANTS FOR 'bfsql'@'%';查看所有权限;
获取当前登陆的用户和数据库:SELECT CURRENT_USER(), SCHEMA();
关于事件计划的权限:
SELECT HOST,USER,Event_priv FROM mysql.user;
上面的例子是从某数据库中查出包含 字段名 字段的所有表名。
判断为空:isnull(`num400`.`ALERT_FARE_400_TIME`)
时间转换和运算:convert(date_format((now() - interval 7 day),'%Y-%m-%d') using utf8)
十进制小数点的切割:decimal(10,4),常常伴随转换--》cast(`fare`.`LEFT_FARE` as decimal(10,4)),cast和convert都是转换数据的,只是convert多了一个参数style
本地连接远程数据库问题:
/data/mysql/bin/mysql -u root -p
grant all privileges on *.* to 'root'@'218.241.181.25' identified by 'hollycrm' with grant option;
grant all privileges on *.* to 'root'@'10.1.10.41' identified by 'hollycrm' with grant option;
flush privileges;
SELECT t.USER_ID _id,t.NAME displayName,t.LOGIN_NAME loginName,t.STATUS enable,t.create_time create_date,t.update_time last_update,t.account_id accountId,t.email email,t.mobile mobile,t1.COMPANY_NAME companyName,t1.REGISTER_NAME accountName FROM `c5_billing`.`c5_account_user` t LEFT JOIN `c5_billing`.`c5_platform_account` t1 ON t.`ACCOUNT_ID`=t1.`ACCOUNT_ID` ORDER BY _id LIMIT 0,40000;
SELECT t.USER_ID _id,t.NAME displayName,t.LOGIN_NAME loginName,t.STATUS enable,t.create_time create_date,t.update_time last_update,t.account_id accountId,t.email email,t.email_is_check emailIsCheck,t.mobile mobile,t.mobile_is_check mobileIsCheck,t1.COMPANY_NAME companyName,t1.REGISTER_NAME accountName FROM `c5_billing`.`c5_account_user` t LEFT JOIN `c5_billing`.`c5_platform_account` t1 ON t.`ACCOUNT_ID`=t1.`ACCOUNT_ID`
联合查询多张表
SELECT * from `update_view` order by _id limit 35001, 40000;
//Java更新MySQL数据库时,如果有就更新,没有就插入:
INSERT INTO … ON DUPLICATE KEY UPDATE …更好;
REPLACE INTO : 如果存在已有记录,将先删除,再插入新数据,效率低,同时会使主键id不断增大;
先SELECT,再确定UPDATE还是INSERT INTO: 效率低,代码量大
public
boolean
uqdateMobileCodeByMobile (MobileCodeQueryVO mobileCode) {
String sql =
"INSERT INTO c5_billing.tbl_mobile_code (mobile,province,city,area_code,post_code,card) VALUES(?,?,?,?,?,?) ON DUPLICATE KEY UPDATE mobile=VALUES(mobile),province=VALUES(province),city=VALUES(city),area_code=VALUES(area_code),post_code=VALUES(post_code),card=VALUES(card) "
;
// String sql = "update c5_billing.tbl_mobile_code SET mobile=?,province=?,city=?,area_code=?,post_code=?,card=? where mobile=?";
List<Object> args =
new
ArrayList<Object>();
args.add(mobileCode.getMobile().trim());
args.add(mobileCode.getProvince().trim());
args.add(mobileCode.getCity().trim());
args.add(mobileCode.getArea_code().trim());
args.add(mobileCode.getPost_code().trim());
args.add(mobileCode.getCard().trim());
// args.add(mobileCode.getMobile().trim());
int
result = 0;
try
{
result =
this
.getMysqlJdbcTemplate().update(sql.toString(), args.toArray());
}
catch
(Exception e) {
logger
.error(
"表c5_billing.tbl_mobile_code数据更新异常!"
, e);
return
false
;
}
if
(result == 1) {
return
true
;
}
return
false
;
}