views:

29

answers:

2

Am getting the following error.

Exception information:

Message: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row

Why does fetchall giving problems for querying multiple rows???

+1  A: 

A possible cause is that the subquery follows '=', '>', ... in an expression and returns more that one row.

Or can you post the SQL statement for further discussion?

ngsiolei
Thanks, that was the answer. But anyways i found the solution before your reply :)
tecks
A: 

This is a MySQL error and not Zend.

You have problem in your SQL (generated SQL). If you are using generated SQL preview the sql by converting the select to string.

Something like this

$db = ''; // Database Adapter

// Build Select
$select = $db->select()
            ->from('tbl_1',
                    array(
                        'id',
                        'count' => '(SELECT count(*) FROM tbl_2 WHERE tbl_2.tbl1_id = tbl_1.id)'
                    )
);

// Output the Query for validation
echo $select->__toString() . PHP_EOL;

I assume you have a select within select, something like:

SELECT id, (SELECT COUNT(*) FROM tbl_2 GROUP BY some_field) as count
FROM tbl_1

Problem is that (SELECT COUNT(*) FROM tbl_2 GROUP BY some_field) returns more than 1 row.

Alex