zoukankan      html  css  js  c++  java
  • MySQL 语句备忘

    删除重复记录的方法,转自:
    http://dadloveu.blog.51cto.com/715500/196309

    找出每个学生的第一个订单
    select * from duplicate where id in(select min(id) from duplicate group by name);
    生成新表:
    create table duplica select * from duplicate where id in(select min(id) from duplicate group by name);
    表改名:
    alter table duplica rename to duplicate;
    别忘了设置主键:
    alter table duplicate modify id int(2) not null primary key auto_increment;

    跨数据库操作
    mysql> create database web;
    mysql> create table test.tableName (name int,pwd int);
    mysql> create table web.table (name int,pwd int);
    mysql> insert into test.tableName (name,pwd) select name,pwd from web.table ;
    mysql> select version();

    判断一个数据库是否存在
    先show databases like 一下,然后根据found_rows()的结果是否为1判断
    show databases like 'a';
    if found_rows()=1 then

    正则regexp
    //匹配以@163.com或@163,com结尾的字符串
    select * from t1 where email regexp "@163[.,]com$";
    更多聚合信息,用with rollup后,不能再用order by
    select id,name,count(id) from t5 group by id,name with rollup;

    explain select count(*) from actif where name like 'a%';
    desc select count(*) from actif where name like 'a%';

    两个简单优化方法
    check table tb1;
    optimize table t1;//整理表碎片,注意不要在工作时间用,尤其在表很大的时候

    导出t5表中的name字段数据
    select * from actif where act_nom like 'a%' into outfile "d:\sql.txt"
    导入数据到t1表中的name字段
    load data infile "d:\sql.txt" into table t1(name);

    因为导入的时候每行一次次导入,并索引;现在要在导入完成后再索引
    alter table t1 name disable keys;//关闭索引
    load data;
    alter table t1 name enable keys;//打开索引

    取消排序,降低损耗,用order by null
    select count(name) from t1 group by name order by null;
    避免子查询,如
    select * from t1 where id in(select id from t2);
    改为这样select t1.name from t1,t2 where t1.id=t2.id;

    // 方案二,一条语句就插入
    INSERT INTO  ff_operateur_detail(op_date, op_username, op_nom, op_prenom, op_tarif, op_ecole, op_fle, op_actif, op_inscription)
    SELECT ins_date,ins_operateur,ins_nom,ins_prenom,ins_recu,ins_ecole,ins_classe_type,ins_actif,ins_id FROM inscription_pan

    左连接:

    SELECT ins_year,ins_month,ins_operateur_id,ins_operateur,SUM(ins_frais+ins_tarif-ins_reduction) AS recu, SUM(fc_recu) AS recu2 FROM inscription_pan
    LEFT JOIN facture_pan
    ON (ins_id=fc_inscription)
    AND (ins_ecole=fc_ecole)
    WHERE (ins_date>='2013-06-01')
    AND (ins_date<='2013-06-10')
    GROUP BY ins_year,ins_month,ins_operateur

    注意,不能把那些where语句放到on里去,完全两回事,而且好像都执行不完。

  • 相关阅读:
    Unity3D 学习笔记
    Python中os和sys模块
    合并两个排序的链表
    反转链表 难
    链表中倒数第k个结点
    调整数组顺序使奇数在偶数前 14
    javascript中this详解
    静态方法实例方法
    强制类型转换
    javascript类型判断方法
  • 原文地址:https://www.cnblogs.com/findumars/p/3090022.html
Copyright © 2011-2022 走看看