mysql sql技巧篇

1、left join 需要注意的事项

  以左表为基准,匹配右表,如果右表匹配了两条,那么,就生成两条记录,而这两条记录的坐表信息都是一样的。

之前误以为,右表不会影响记录的条数。select 部分,不再是两张表的概念,而是一个大临时表。比如select t2.count(id)  ,会只生成一条记录。

2、order by子句--执行顺序为从左到右排序,很耗资源

3、group by--执行顺序从左往右分组,group by之后在筛选的话用having,例如筛选出分组数大于1的:select count(*) as num from t1 where .. group by t1.id having num>1 

4、where子句--执行顺序为自下而上、从右到左

5、联表的时候,小表(筛选条件后)作为驱动表(后表)

Ps:小表遍历根据两表的关联条件去找大表对应的数据,很快就能找的,有索引就更快了。反之,大表本身遍历就很麻烦,就算有索引,也得遍历一遍大表。

6、count(*) 和 group by 一起使用,count(*) 的值 会有多条,而且是group by分组后各个组的条数,而且,如果没有记录的话,也会返回一条记录,但是这条记录的count(*) =0

7、查询订单中每一天充值的总人数,总次数,总金额,总元宝(由游戏表金额元宝兑换率转换)。

SELECT COUNT(*) AS countUser, createdTime, countNum, countAmount, countGold
FROM (SELECT COUNT(*) AS countNum, SUM(t1.cd_Amount) AS countAmount, SUM(t1.cd_Amount * t2.sd_ExchangeRate) AS countGold, FROM_UNIXTIME(cd_CreatedTime, '%Y-%m-%d')
AS createdTime FROM CD_Order t1 LEFT JOIN SD_GameInfo t2 ON t1.ga_Gameid = t2.ga_GameID WHERE t1.cd_PayStatus = 1 AND t1.cd_SendStatus != 0 AND t1.cd_CreatedTime >= ? AND t1.cd_CreatedTime <= ? GROUP BY t1.pt_AccountKey, createdTime ) t GROUP BY t.createdTime

然后,程序得出每一天的数组。

 $chartData = array(); 

while (strtotime($start) <= strtotime($enddate)) { $chartData[]['createTime'] = $start; $start = date('Y-m-d', strtotime($start . " +1 day")); }


最后,根据你的需求,整合这两个数组既可。

8、同步原ms库中主题表
self::$db->query('truncate fg_cms.question_topic');
self::$db->query('insert into fg_cms.question_topic select * from platform.CS_QuestionTopic');

9、把一个表的字段复制到另外一表

 update t1,t2 set t1.na=t2.na where t1.id=t2.id 
 
查看mysql状态:show status;
查看系统变量:show variables; show variables like '%max_connections%';
修改系统变量:set global max_connections=14000;

    将文件中的“bind-address  = 127.0.0.1”改为“bind-address  = 0.0.0.0”,让所有IP都能访问

  

linux下导入、导出mysql数据库命令

一、导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):
1、导出数据和表结构:
mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql
#/usr/local/mysql/bin/   mysqldump -uroot -p abc > abc.sql
敲回车后会提示输入密码

2、只导出表结构
mysqldump -u用户名 -p密码 -d 数据库名 > 数据库名.sql
#/usr/local/mysql/bin/   mysqldump -uroot -p -d abc > abc.sql

mysqldump -uroot -p123456 --opt --skip-extended-insert --single-transaction --master-data=2 -R dbname | gzip >/data/dbname.sql.gz

注:/usr/local/mysql/bin/  --->  mysql的data目录

3、只导出某张表

mysqldump -u用户名 -p密码 数据库名 数据表名 > 数据表名.sql


二、导入数据库
1、首先建空数据库
mysql>create database abc;

2、导入数据库
方法一:
(1)选择数据库
mysql>use abc;
(2)设置数据库编码
mysql>set names utf8;
(3)导入数据(注意sql文件的路径)
mysql>source /home/abc/abc.sql;
方法二:
mysql -u用户名 -p密码 数据库名 < 数据库名.sql
#mysql -uabc_f -p abc < abc.sql

 数据复制迁移

1、复制datadir下面的数据库文件夹和mysql文件夹以及ibdata1文件

2、删除原来的ib_logfile0,ib_logfile1

3、重启mysql

如果出现类似错误,证明是复制文件的时候,ibdata1和具体所有文件不对称导致的

2013-11-04 10:02:28 2aaf5d032520  InnoDB: Operating system error number 2 in a file operation.
InnoDB: The error means the system cannot find the path specified.
InnoDB: If you are installing InnoDB, remember that you must create
InnoDB: directories yourself, InnoDB does not create them.
InnoDB: Error: could not open single-table tablespace file ./mysql/innodb_table_stats.ibd
InnoDB: We do not continue the crash recovery, because the table may become
InnoDB: corrupt if we cannot apply the log records in the InnoDB log to it.
InnoDB: To fix the problem and start mysqld:

 解决方法:mysqld下增加innodb_force_recovery=1

重启,导出数据库,再导进去一次,然后innodb_force_recovery=0还原。重启即可

不同表更新字段

update user.cdb_user t1,tutu.cdb_area t2 set t1.parentAreaId = t2.upid where t1.areaId = t2.id

  

监控mysql堆积情况(这样就把堆积会话数取下来的)

select count(*) from information_schema.processlist where Command<>'Sleep';

  

暗夜之中,才见繁星;危机之下,暗藏转机;事在人为,为者常成。
原文地址:https://www.cnblogs.com/zenghansen/p/4036788.html