Friday, 22 June 2012

Resolve Subquery returns more than one row in SQL

Subquery can be  used to return records that will be used in the main query as a condition to further restrict the records to be retrieved.
While you retriving data from database you can get error like  "Subquery returns more than one row ".That is subquery returns more than one row for your condtion.
In main query means that will not affect.In subquery will return only one row at that time.For that we can use "GROUP_CONCAT" for combining more than one row as a single row.now that error will gone away.

Example:

SELECT
     ID as id,
     (SELECT NAME  FROM DEPARTMENT where ID='1') as name,
FROM
     ASSOCIATION
WHERE
   NAME LIKE "%raja%"

in that ID=1 having more than one data means return more rows.For you can use like that
 GROUP_CONCAT(SELECT NAME  FROM DEPARTMENT where ID='1') as name means it will return one row with comma separated values..(ram,raj,raja) like that









No comments:

Post a Comment