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;

 

你可能感兴趣的:(mysql开发常用小结)