1 插入数据

    这条语句可以同时插入多条数据,若为所有字段插入数据,则column_list可以省略掉。
    INSERT INTO table_name (column_list) VALUES (value_list1)[, (value_list2)...];    

2 修改数据

    UPDATE table_name 
     SET column_name1 = value1,
     column_name2=value2,……,
     column_namen=valueN
     WHERE (condition);

3 删除数据

    若无where 条件则删除所有数据
    DELETE FROM table_name [WHERE condition] ;

4 查询数据

    a 单表查询
    语法规范:
        SELECT {*|<字段列表>} [
            from <表1>, <表2>...
            [where <condition>]
            [group by <group by definition>]
            [having <expression> [{<operator> <expression>...}]]
            [order by <order by definition>]
            [limit [<offset>,] <row count>]
        ]
        1. 查询所有字段
            select * from mytable1;
        2. 查询指定字段
            select id, name from mytable1;
        3. 筛选
            select id, name from mytable1 where id=3;
            select id, name from mytable1 where id in (1, 2);
            select id, name from mytable1 where id between 1 and 3; //这里包括1-3之间的所有值
            select id, name from mytable1 where name like 'b%';
            select id, name from mytable1 where name like 'b__';
            select id, name from mytable1 where name is not null;
            select id, name from mytable1 where id in (1, 2) and name is not null;
            select id, name from mytable1 where id=1 or id=2; //可以实现in功能,不过in效率更高
            select distinct name from mytable1;  //消除重复值
        4. 排序,desc只对其前面的一个字段生效,如果要对每一个字段都降序,则每个字段后都要加desc
            单列排序
            select age from mytable1 order by age;
            多列排序:
            select age, score from mytable1 order by score, age;
        5. 分组
            select id, group_concat(name) as names from mytable1 group by id having count(name)>1;
            统计分组数目:
            group by id with rollup;(该命令无法与order by一起使用)
        6. 限制返回结果
            limit 3,5; //第四行开始,共5行
        7. 集合函数
            count(), sum(), avg(),min(), max()等等,他们不包含NULL所在的行。
    b 多表查询
        1. 内连接查询
            select mytable1.id, mytable1.name, mytable2.id, mytable2.name
                from mytable1 inner join mytable2
                on mytable1.id=mytable2.id;
        2. 外连接查询
            左连接
            select mytable1.id, mytable1.name, mytable2.id, mytable2.name
                from mytable1 left outer join mytable2
                on mytable1.id=mytable2.id;
            右连接
            select mytable1.id, mytable1.name, mytable2.id, mytable2.name
                from mytable1 right outer join mytable2
                on mytable1.id=mytable2.id;
    c 子查询
        1. ANY, SOME为同义词,表示有一个满足即可
        2. all 表示要满足所有条件才行
        3. exits 存在为true, 否则为false
        4. in 内层查询返回列表与外层比较
        5. 带比较运算符的子查询
    d 合并查询
        union 重复的行被删除
        union all 重复的行不被删除,效率比union高
    e 使用正则表达式查询regexp
        select * from mytable1 where name regexp '^b*a$';

posted on 2016-02-22 20:50  迷阳  阅读(180)  评论(0编辑  收藏  举报