How can I select from list of values in SQL Server

后端 未结 14 1706
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 18:08

I have very simple problem that I can\'t solve. I need to do something like this:

select distinct * from (1, 1, 1, 2, 5, 1, 6).

Anybody can

相关标签:
14条回答
  • 2020-11-28 18:43

    I know this is a pretty old thread, but I was searching for something similar and came up with this.

    Given that you had a comma-separated string, you could use string_split

    select distinct value from string_split('1, 1, 1, 2, 5, 1, 6',',')
    

    This should return

    1
    2
    5
    6
    

    String split takes two parameters, the string input, and the separator character.

    you can add an optional where statement using value as the column name

    select distinct value from string_split('1, 1, 1, 2, 5, 1, 6',',')
    where value > 1
    

    produces

    2
    5
    6
    
    0 讨论(0)
  • 2020-11-28 18:48

    If you want to select only certain values from a single table you can try this

    select distinct(*) from table_name where table_field in (1,1,2,3,4,5)
    

    eg:

    select first_name,phone_number from telephone_list where district id in (1,2,5,7,8,9)
    

    if you want to select from multiple tables then you must go for UNION.

    If you just want to select the values 1, 1, 1, 2, 5, 1, 6 then you must do this

    select 1 
    union select 1 
    union select 1 
    union select 2 
    union select 5 
    union select 1 
    union select 6
    
    0 讨论(0)
  • 2020-11-28 18:48

    PostgreSQL gives you 2 ways of doing this:

    SELECT DISTINCT * FROM (VALUES('a'),('b'),('a'),('v')) AS tbl(col1)
    

    or

    SELECT DISTINCT * FROM (select unnest(array['a','b', 'a','v'])) AS tbl(col1)
    

    using array approach you can also do something like this:

    SELECT DISTINCT * FROM (select unnest(string_to_array('a;b;c;d;e;f;a;b;d', ';'))) AS tbl(col1)
    
    0 讨论(0)
  • 2020-11-28 18:50

    In general :

    SELECT 
      DISTINCT 
          FieldName1, FieldName2, ..., FieldNameN
    FROM
      (
        Values
            ( ValueForField1, ValueForField2,..., ValueForFieldN ),
            ( ValueForField1, ValueForField2,..., ValueForFieldN ),
            ( ValueForField1, ValueForField2,..., ValueForFieldN ),
            ( ValueForField1, ValueForField2,..., ValueForFieldN ),
            ( ValueForField1, ValueForField2,..., ValueForFieldN )
      ) AS TempTableName ( FieldName1, FieldName2, ..., FieldNameN )
    

    In your case :

    Select 
      distinct
      TempTableName.Field1 
    From 
      (
      VALUES
        (1), 
        (1), 
        (1), 
        (2), 
        (5), 
        (1), 
        (6)
      ) AS TempTableName (Field1)
    
    0 讨论(0)
  • 2020-11-28 18:50

    Have you tried using the following syntax?

    select * from (values (1), (2), (3), (4), (5)) numbers(number)
    
    0 讨论(0)
  • 2020-11-28 18:50

    Use the SQL In function

    Something like this:

    SELECT * FROM mytable WHERE:
    "VALUE" In (1,2,3,7,90,500)
    

    Works a treat in ArcGIS

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