How to select distinct rows without using group by statement

前端 未结 4 590
挽巷
挽巷 2021-01-01 19:28
A B C
1 1 1 
1 1 1 
2 2 2 
2 2 2 
3 3 3 
3 3 3 
4 4 4 
4 4 4 
5 5 5 
5 5 5 
5 5 5 
6 6 6
6 6 6 

I am to output only the distinct rows without using

相关标签:
4条回答
  • 2021-01-01 19:53
    SELECT DISTINCT A,B,C FROM TABLE;
    

    According to mysql documentation, DISTINCT specifies removal of duplicate rows from the result set (http://dev.mysql.com/doc/refman/5.0/en/select.html)

    I created a sample on jsfiddle and it works IMHO

    create table tablea (A int,B int,C int);
    create table tableb (A int);
    
    INSERT INTO tablea VALUES (1,1,1),(1,1,1),(2,2,2),(2,2,2),(3,3,3),(3,3,3),(4,4,4),(4,4,4),(5,5,5),(5,5,5),(5,5,5),(6,6,6),(6,6,6);
    INSERT INTO tableb VALUES (1),(1),(1),(1),(1);
    
    SELECT DISTINCT tablea.A,tablea.B,tablea.C FROM tablea INNER JOIN tableb ON tablea.A=tableb.A;
    

    feel free to experiment on this SQLFiddle.

    0 讨论(0)
  • 2021-01-01 19:54

    IF you (or anyone else) really wants to get distinct resutl without using DISTINCT or GROUP BY statement, this may be the way:

    SELECT
        a.name
    FROM person a
    LEFT JOIN person b
        ON (a.name = b.name OR (a.name IS NULL AND b.name IS NULL))
        AND a.id < b.id
    WHERE b.id IS NULL
    

    It's rather a quaint thing, though.

    0 讨论(0)
  • 2021-01-01 19:55

    the DISTINCT keyword does what you want. I.e., if the name of your table is A, select distinct * from A will do the trick.

    Cf. mysql manual: http://dev.mysql.com/doc/refman/5.0/en/select.html

    0 讨论(0)
  • 2021-01-01 20:06

    This may totally shock you but MySQL uses GROUP BY under the hood to execute DISTINCT !!!

    Here is something you may want to try

    If the table is called mytable, do these two things:

    First run this

    ALTER TABLE mytable ADD INDEX ABC (A,B,C);`
    

    Second, run this query

    SELECT A,B,C FROM mytable GROUP BY A,B,C;
    

    GROUP BY actually works better with an index present !!!

    Here is sample code to prove it works

    mysql> drop database if exists cool_cs;
    Query OK, 1 row affected (0.04 sec)
    
    mysql> create database cool_cs;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use cool_cs
    Database changed
    mysql> create table mytable
        -> (A int,B int,C int, key ABC (A,B,C));
    Query OK, 0 rows affected (0.08 sec)
    
    mysql> INSERt INTO mytable VALUES
        -> (1,1,1),(1,1,1),(2,2,2),(2,2,2),(3,3,3),
        -> (3,3,3),(4,4,4),(4,4,4),(5,5,5),(5,5,5),
        -> (5,5,5),(6,6,6),(6,6,6);
    Query OK, 13 rows affected (0.06 sec)
    Records: 13  Duplicates: 0  Warnings: 0
    
    mysql> select a,b,c FROM mytable group by a,b,c;
    +------+------+------+
    | a    | b    | c    |
    +------+------+------+
    |    1 |    1 |    1 |
    |    2 |    2 |    2 |
    |    3 |    3 |    3 |
    |    4 |    4 |    4 |
    |    5 |    5 |    5 |
    |    6 |    6 |    6 |
    +------+------+------+
    6 rows in set (0.02 sec)
    
    mysql>
    

    Give it a Try !!!

    0 讨论(0)
提交回复
热议问题