SQL select distinct substring where like muddleup howto

后端 未结 4 1953
情书的邮戳
情书的邮戳 2021-01-02 17:47

I\'ve got a table with a field that is similar to this:

ANIMAL
========
FISH 54
FISH 30
DOG 12
CAT 65
CAT 09
BIRD 10
FISH 31
DOG 10

The fie

相关标签:
4条回答
  • 2021-01-02 18:01

    You can also use the following to get your list of animal names:

    SELECT DISTINCT SUBSTRING_INDEX(animal, ' ', 1) FROM table_name;
    
    0 讨论(0)
  • 2021-01-02 18:02

    This will work for SQL Server. If you use something else you need to figure out the corresponding functions to left and charindex. Left could of course be replaced with a substring.

    select distinct left(T.Animal, charindex(' ', T.Animal, 1)-1)
    from YourTable as T
    

    Result:

    -------------------------
    BIRD
    CAT
    DOG
    FISH
    

    In MySQL you would use left and locate. (Code not tested)

    select distinct left(T.Animal, locate(' ', T.Animal, 1)-1)
    from YourTable as T
    
    0 讨论(0)
  • 2021-01-02 18:05
    SELECT Name, Max(No)
    FROM Table
    Group By Name
    
    0 讨论(0)
  • 2021-01-02 18:09

    Why not just

    SELECT id, animal, value FROM table GROUP BY animal, id HAVING id = MIN(id)
    

    That should get you a list of the animals in the table, each animal with the first entered value.

    If you don't need so select the value, then just do:

    SELECT animal FROM table GROUP BY animal

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