Solution to “subquery returns more than 1 row” error

前端 未结 4 1717
囚心锁ツ
囚心锁ツ 2020-11-27 02:40

I have one query that returns multiple rows, and another query in which I want to set criteria to be either one of values from those multiple rows , so basicly I want the su

4条回答
  •  忘掉有多难
    2020-11-27 03:33

    = can be used when the subquery returns only 1 value.

    When subquery returns more than 1 value, you will have to use IN:

    select * 
    from table
    where id IN (multiple row query);
    

    For example:

    SELECT *
    FROM Students
    WHERE Marks = (SELECT MAX(Marks) FROM Students)   --Subquery returns only 1 value
    
    SELECT *
    FROM Students
    WHERE Marks IN 
          (SELECT Marks 
           FROM Students 
           ORDER BY Marks DESC
           LIMIT 10)                       --Subquery returns 10 values
    

提交回复
热议问题