MySQL Notes 03

distinct关键字:
 
来源:<http://blog.csdn.net/maray/article/details/7634543>
 
通过distinct关键字能够过滤掉结果集中重复的行。
 
例如:
 
    mysql> select * from ob;
    +------+------+------+
    | a    | b    | c    |
    +------+------+------+
    |    1 |    2 |    3 |
    |    1 |    2 |    3 |
    |    1 |    2 |    3 |
    |    1 |    2 | 2323 |
    |    1 |  323 |   21 |
    |    9 |    9 |    9 |
    +------+------+------+
 
    /// 取出所有不同行:
 
    mysql> select distinct * from ob;
 
    +------+------+------+
    | a    | b    | c    |
    +------+------+------+
    |    1 |    2 |    3 |
    |    1 |    2 | 2323 |
    |    1 |  323 |   21 |
    |    9 |    9 |    9 |
    +------+------+------+
 
    /// 取出所有a, b列不相同的行
 
    mysql> select distinct a, b from ob;
 
    +------+------+
    | a    | b    |
    +------+------+
    |    1 |    2 |
    |    1 |  323 |
    |    9 |    9 |
    +------+------+
 
___________________________________________________________________________________________________________________
 
group_concat函数:
 
来源:<http://www.poluoluo.com/jzxy/200812/53698.html>
 
group_concat(列名)
group_concat(列名 separator '分割符')
 
group_concat函数会计算哪些行属于同一组,将属于同一组的相关信息显示出来。
 
★注意:group_concat()函数需要与group by语句在一起使用,才能得到需要的效果。
 
例如:
 
    mysql> select * from aa;
 
    +---+------+
    | id| name |
    +---+------+
    | 1 | 10 |
    | 1 | 20 |
    | 1 | 20 |
    | 2 | 20 |
    | 3 | 200  |
    | 3 | 500  |
    +---+------+
 
    /// 以id分组,把name字段的值打印在一行,逗号分隔(默认):
 
    mysql> select id,group_concat(name) from aa group by id;
 
    +---+--------------------+
    | id| group_concat(name) |
    +---+--------------------+
    | 1 | 10,20,20 |
    | 2 | 20 |
    | 3 | 200,500 |
    +---+--------------------+
 
    /// 以id分组,把name字段的值打印在一行,分号分隔:
 
    mysql> select id,group_concat(name separator ';') from aa group by id;
 
    +---+----------------------------------+
    | id| group_concat(name separator ';') |
    +---+----------------------------------+
    | 1 | 10;20;20 |
    | 2 | 20|
    | 3 | 200;500  |
    +---+----------------------------------+
 
    /// 以id分组,过滤掉name字段中重复的值:
 
    mysql> select id,group_concat(distinct name) from aa group by id;
 
    +---+-----------------------------+
    | id| group_concat(distinct name) |
    +---+-----------------------------+
    | 1 | 10,20|
    | 2 | 20  |
    | 3 | 200,500 |
    +---+-----------------------------+
 
    /// 以id分组,把name字段的值打印在一行,以name排倒序 :
 
    mysql> select id,group_concat(name order by name desc) from aa group by id;
 
    +---+---------------------------------------+
    | id| group_concat(name order by name desc) |
    +---+---------------------------------------+
    | 1 | 20,20,10  |
    | 2 | 20|
    | 3 | 500,200|
    +---+---------------------------------------+
 
 
MySQL 列的增、删、改:
 
增加列:
    alter table 表名 add 【字段名 字段属性】;
 
    (新增的字段,默认排在表的最后一列)
 
  通过 after 来声明新增的字段在哪一字段的后面:
 
    alter table 表名 add 【字段名 字段属性】 after 前一个字段的字段名;
 
  通过 first 来声明新增的字段,让其排在最前面:
 
    alter table 表名 add 【字段名 字段属性】 first;
 
 
修改列:
    alter table 表名 change 被修改字段的字段名 【字段名 字段属性】;
 
 
删除列:
    alter table 表名 drop 字段名;

 

posted @ 2017-11-10 20:37  Uncle_Jay  阅读(85)  评论(0编辑  收藏  举报