subquery using IN clause

前端 未结 4 1668
[愿得一人]
[愿得一人] 2021-01-15 06:02
----------
User
----------
user_ID(pk)
UserEmail

----------
Employer1
----------
Emp1ID(pk)
Emp1NO

----------
Employer2
----------
Emp2ID(pk)
Emp2NO

----------
Pr         


        
相关标签:
4条回答
  • 2021-01-15 06:40

    You can only return one column from your subquery. If you just want both the employee ID and manager ID consider a union query as your subquery:

        emp.Emp2NO IN
    (select ProjEmpID from project
     union
     select ProjEmpMGRID from project)
    

    or rewriting to use two IN queries with separate subqueries for each.

    0 讨论(0)
  • 2021-01-15 06:50
    emp.Emp2NO IN
     (select ProjEmpID from project) or (select ProjEmpMGRID from project)
    

    Hope you will understant why it should be so. u can put a mail to me at gnmsr@ap.nic.in

    Ok i will expalin. Emp1NO is one value chcked with every value in the list of values return by a single column values where as ur query returns a matrix of many(no. of rows) X 2

    0 讨论(0)
  • 2021-01-15 06:52

    I would recommend not using the IN statement. Use EXISTS instead.

    http://www.techonthenet.com/sql/exists.php

    Example #1

    0 讨论(0)
  • 2021-01-15 06:57

    Eric Hauser is correct - you can't specify two columns in SELECT contained in an IN clause to one column outside.

    That said, I re-wrote your query so you don't need to use UNIONs at all:

    SELECT DISTINCT u.user_email
      FROM USERS u
      JOIN EMPLOYER e ON u.user_id IN (e.emp1id, e.emp2id) 
      JOIN PROJECT p ON e.emp1no IN (p.projempid, p.projempmgrid)
                     OR e.emp2no IN (p.projempid, p.projempmgrid)
    

    I also changed the query to use ANSI-92 JOIN syntax - your original uses ANSI-89.

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