zoukankan      html  css  js  c++  java
  • 查询

    Enter password: ******
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 11
    Server version: 5.1.55-community MySQL Community Server (GPL)

    Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
    This software comes with ABSOLUTELY NO WARRANTY. This is free software,
    and you are welcome to modify and redistribute it under the GPL v2 license

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql> use test_1
    Database changed
    mysql> select * from student;
    ERROR 1146 (42S02): Table 'test_1.student' doesn't exist
    mysql> select * from students;
    +--------+----------+---------+
    | stu_id | stu_name | stu_sex |
    +--------+----------+---------+
    | 1 | xiaobai | F |
    +--------+----------+---------+
    1 row in set (0.01 sec)

    mysql> select * from students;
    +--------+----------+---------+
    | stu_id | stu_name | stu_sex |
    +--------+----------+---------+
    | 1 | xiaobai | F |
    | 2 | xiaobai | F |
    | 3 | xiaobai | F |
    | 4 | xiao | F |
    | 5 | xiaoming | F |
    +--------+----------+---------+
    5 rows in set (0.00 sec)

    mysql> select stu_id, stu_name, stu_sex from students group by stu_name;
    +--------+----------+---------+
    | stu_id | stu_name | stu_sex |
    +--------+----------+---------+
    | 4 | xiao | F |
    | 1 | xiaobai | F |
    | 5 | xiaoming | F |
    +--------+----------+---------+
    3 rows in set (0.01 sec)

    mysql> select distinct stu_name from students;
    +----------+
    | stu_name |
    +----------+
    | xiaobai |
    | xiao |
    | xiaoming |
    +----------+
    3 rows in set (0.00 sec)

    mysql> select distinct stu_name, stu_id, stu_sex from students;
    +----------+--------+---------+
    | stu_name | stu_id | stu_sex |
    +----------+--------+---------+
    | xiaobai | 1 | F |
    | xiaobai | 2 | F |
    | xiaobai | 3 | F |
    | xiao | 4 | F |
    | xiaoming | 5 | F |
    +----------+--------+---------+
    5 rows in set (0.00 sec)

    mysql> select *, count(distinct stu_name) from students group by stu_name;
    +--------+----------+---------+--------------------------+
    | stu_id | stu_name | stu_sex | count(distinct stu_name) |
    +--------+----------+---------+--------------------------+
    | 4 | xiao | F | 1 |
    | 1 | xiaobai | F | 1 |
    | 5 | xiaoming | F | 1 |
    +--------+----------+---------+--------------------------+
    3 rows in set (0.01 sec)

    mysql>

    目的:从表中筛选出不重复的

    1.从表中仅仅将某一列的不重复项给筛选出来
    select distinct stu_name from students;
    仅仅将students表中stu_name列里不重复的项给筛选出来

    2.从表中将某一列的不重复项给筛选出来,同时其关联的其它列也一起显示出来
    select stu_id, stu_name, stu_sex from students group by stu_name;
    使用的是group by
    note:重复项保留的是最先出现的那一行

    3.select *, count(distinct stu_name) from students group by stu_name;
    会在输出的地方添加count(distinct stu_name)列

    4.select distinct stu_name, stu_id, stu_sex from students;
    会改变列的列号

    5.select *, count(distinct stu_name) from students;

    mysql> select *, count(distinct stu_name) from students;
    +--------+----------+---------+--------------------------+
    | stu_id | stu_name | stu_sex | count(distinct stu_name) |
    +--------+----------+---------+--------------------------+
    | 1 | xiaobai | F | 3 |
    +--------+----------+---------+--------------------------+
    1 row in set (0.00 sec)

  • 相关阅读:
    mysql使用笔记
    js this指向练习题
    for in 和for of的区别
    ES6 Iterator(迭代器或遍历器)和 Generator (生成器)
    vue兄弟之间传值 bus中央事件总线
    关于recycleview 滑动item变长(item变形)问题
    蓝牙篇
    如何限制应用安装,
    网络工具,通过该类可以直接监听网络状态改变
    MyRecycleView带有上拉加载更多
  • 原文地址:https://www.cnblogs.com/qbin/p/10041523.html
Copyright © 2011-2022 走看看