mysql技术积累

1,中文排序
order by convert(A.name using gb2312)
或者
order by convert(A.name using gbk) desc
2,导出数据库的某张表,或多张表(空格分开)

mysqldump -u root -p123 db table1 table2 table3 > D:\tables\xxx.sql
3,导入某张表
mysql -u root -p123 db < D:\tablefile.sql

4,mysql同表同字段合并用group_concat函数
select
(select
            group_concat(name)
        from
            user
        where
            id in (
                SELECT
                    recv_userid
                FROM
                    oa_smsrecv
                where
                    sms_id = A.sms_id
            )
)
from oa_sms A

5, mysql union 的错误- illegal mix of collations for operation "union"  的 解决方法
 
  联合查询Illegal mix of collations for operation 'UNION'问题

添加unhex(hex(要处理的字段或子查询等))


结果:
张三,李四,王五

6,mysql 做合计,总计, group by XXX with rollup
select id,sum(cost1),sum(cost2),sum(cost1+cost2) from

(SELECT A.id,
(select (case when sum(smoney) is null then 0 else sum(smoney) end) from oa_vservice where vehicle_id=A.id) cost1,
(select (case when sum(gascost) is null then 0 else sum(gascost) end) from oa_vspinreg where vehicle_id=A.id) cost2

FROM oa_vehicle A
where A.isenabled=1
group by A.id) vscost
  group by id with rollup ;

7,授权
GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'root-password' WITH GRANT OPTION;

8,查看用户权限
show grants for root@'localhost';

9,导出数据结构,不导数据
mysqldump -uroot -proot -d dbname >dbname.sql

10,锁表dump
--lock-tables --lock-all-tables

11,selec into outfile 写文件权限问题
查看这个文件
/etc/apparmor.d/usr.sbin.mysqld

12,mysql 到处某表下某条件的数据
mysqldump -uroot -p --where='id=79470046826' fabu article > article.sql

你可能感兴趣的:(sql,mysql)