SQL multiple columns in IN clause

后端 未结 6 1977
情书的邮戳
情书的邮戳 2020-11-27 04:15

If we need to query a table based on some set of values for a given column, we can simply use the IN clause.

But if query need to be performed based on multiple col

相关标签:
6条回答
  • 2020-11-27 04:52

    You could do like this:

    SELECT city FROM user WHERE (firstName, lastName) IN (('a', 'b'), ('c', 'd'));
    

    The sqlfiddle.

    0 讨论(0)
  • 2020-11-27 04:54

    In general you can easily write the Where-Condition like this:

    select * from tab1
    where (col1, col2) in (select col1, col2 from tab2)
    

    Note
    Oracle ignores rows where one or more of the selected columns is NULL. In these cases you probably want to make use of the NVL-Funktion to map NULL to a special value (that should not be in the values):

    select * from tab1
    where (col1, NVL(col2, '---') in (select col1, NVL(col2, '---') from tab2)
    

    oracle sql

    0 讨论(0)
  • 2020-11-27 05:00

    Determine whether the list of names is different with each query or reused. If it is reused, it belongs to the database.

    Even if it is unique with each query, it may be useful to load it to a temporary table (#table syntax) for performance reasons - in that case you will be able to avoid recompilation of a complex query.

    If the maximum number of names is fixed, you should use a parametrized query.

    However, if none of the above cases applies, I would go with inlining the names in the query as in your approach #1.

    0 讨论(0)
  • 2020-11-27 05:02

    Ensure you have an index on your firstname and lastname columns and go with 1. This really won't have much of a performance impact at all.

    EDIT: After @Dems comment regarding spamming the plan cache ,a better solution might be to create a computed column on the existing table (or a separate view) which contained a concatenated Firstname + Lastname value, thus allowing you to execute a query such as

    SELECT City 
    FROM User 
    WHERE Fullname in (@fullnames)
    

    where @fullnames looks a bit like "'JonDoe', 'JaneDoe'" etc

    0 讨论(0)
  • 2020-11-27 05:03

    It often ends up being easier to load your data into the database, even if it is only to run a quick query. Hard-coded data seems quick to enter, but it quickly becomes a pain if you start having to make changes.

    However, if you want to code the names directly into your query, here is a cleaner way to do it:

    with names (fname,lname) as (
        values
            ('John','Smith'),
            ('Mary','Jones')
    )
    select city from user
        inner join names on
            fname=firstName and
            lname=lastName;
    

    The advantage of this is that it separates your data out of the query somewhat.

    (This is DB2 syntax; it may need a bit of tweaking on your system).

    0 讨论(0)
  • 2020-11-27 05:09

    In Oracle you can do this:

    SELECT * FROM table1 WHERE (col_a,col_b) IN (SELECT col_x,col_y FROM table2)
    
    0 讨论(0)
提交回复
热议问题