mysql开发常用小结
1.时间转换
时间戳转时间 FROM_UNIXTIME
比如 FROM_UNIXTIME(1449480551/1000,'%Y-%m-%d %H:%i:%s')
如果是精确到毫秒的时间戳,则必须除1000 比如 FROM_UNIXTIME(1449480551850/1000,'%Y-%m-%d %H:%i:%s')
如果时间格式不对,则会返回null
时间转时间戳 UNIX_TIMESTAMP
select UNIX_TIMESTAMP( date('2015-10-25 18:20:25'))
select UNIX_TIMESTAMP( ),UNIX_TIMESTAMP( NOW()) --返回结果一样
2.datetime与timestamp区别
+
---------------+----------------+
| 列类型 | 显示格式 |
|
TIMESTAMP
(14) | YYYYMMDDHHMMSS |
|
TIMESTAMP
(12) | YYMMDDHHMMSS |
|
TIMESTAMP
(10) | YYMMDDHHMM |
|
TIMESTAMP
(8) | YYYYMMDD |
|
TIMESTAMP
(6) | YYMMDD |
|
TIMESTAMP
(4) | YYMM |
|
TIMESTAMP
(2) | YY |
+
---------------+----------------+
范围
datetime 以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值。支持的范围为'1000-01-01 00:00:00'到'9999-12-31 23:59:59'TIMESTAMP值不能早于1970或晚于2037
储存
TIMESTAMP
1.4个字节储存(Time stamp value is stored in 4 bytes)
2.值以UTC格式保存( it stores the number of milliseconds)
3.时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
datetime
1.8个字节储存(8 bytes storage)
2.实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)
3.与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)
timestamp具有强制将长度上提到偶数的能力,同时,存储的还是14位数据,只不过读取的时候,隐藏了部分,如果将长度设置成14位,则可以看到隐藏的部分
另外timestamp默认非null,但可以手动设置和赋值
更多:http://www.jb51.net/article/51794.htm
3.子查询更新
update tb a,
(select time,name
from tt )b
set time4=b.col
where a.name=b.name and a.time1=b.time;
4.查询表结构相关语句
/**查看表结构**/ desc yourtablename /**查看创建表语句**/ show create table yourtablename /**查看所有列的信息**/ use information_schema; select * from columns where table_name='yourtablename'; /**查看所有列名的信息**/ use information_schema; select column_name from columns where table_name='yourtablename'; /**拼接列名到预定义的sql**/ select concat('insert into yourtablename values(',r.column_name) from (select group_concat(column_name) column_name from columns where table_name='yourtablename') r; /**查询mysql中包含指定列的所有表名称和注释**/ useinformation_schema; selectdistinctc.table_name,t.TABLE_COMMENTfromcolumnscleftjointablest onc.table_name=t.TABLE_NAME wherec.TABLE_SCHEMA='database'/**数据库名称*/ andc.COLUMN_NAME='password'/**列名称*/ andc.DATA_TYPE='int'/**数据列类型*/
5.如何判断字段中是否含有中文
SELECT title,length(title),CHARACTER_LENGTH(title) FROM `temp_demo`
6.ROWNUM
select * from (SELECT @rownum:=@rownum+1 rownum, identifier From (SELECT @rownum:=0,contributes.* FROM contributes limit 10) t)a where rownum>4 and rownum<6
7. 5.6 版本安装
1.配置path
2.设置my-default.ini
basedir = D:\download\tools\develop\mysql-5.6.24-winx64\bin
datadir = D:\download\tools\develop\mysql-5.6.24-winx64\data
3.安装 mysqld -install
启动net stat mysql
设置root空密码
SET PASSWORD FOR username=PASSWORD('new password');
或则
mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='username';
mysql> FLUSH PRIVILEGES;
8. 开启远程监听
mysql -u root -pvmwaremysql>use mysql;mysql>update user set host = '%' where user = 'root';mysql>select host, user from user;
或则
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
9.时间默认设置
你可以改成 publis_date date not null default curdate();
curdate() 当前系统的时间,date 类型
sysdate() 和 now() 是当前系统时间并且精确到时分秒的,也就是datetime类型
TIMESTAMP DEFAULT CURRENT_TIMESTAMP
10. MySQL问题解决:-bash:mysql:command not found
ln -s /usr/local/mysql/bin/mysql /usr/bin 做个链接即可
重启mysql命令:service mysql start
11.com.mysql.jdbc.PacketTooBigException: Packet for query is too large (2289 >1024)
max_allowed_packet 值太小造成
执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。
比如,图片数据的处理
在my.cnf文件中添加或者修改以下变量:
max_allowed_packet = 10M (也可以设置自己需要的大小)
max_allowed_packet 参数的作用是,用来控制其通信缓冲区的最大长度。
查看目前配置
show VARIABLES like '%max_allowed_packet%';
在mysql 命令行中运行
set global max_allowed_packet = 2*1024*1024*10
然后关闭掉这此mysql server链接,再进入。
参考:
http://www.cnblogs.com/huangcong/archive/2013/03/26/2981790.html
http://www.oschina.net/question/1422726_2144185