mohammad ali Payan mohammad ali Payan - 1 year ago 55
SQL Question

How do i fix subquery returns more than 1 row error

I am executing this query

$sql=mysql_query("SELECT c.*,
(SELECT Count(b.text) From tbl_people_comment Where AND b.visible=1) AS ccomment
FROM tbl_peoplesms AS c
INNER JOIN tbl_people_comment AS b ON b.idsms=
WHERE c.visible=1

I am trying to make it return something like this in php



i use this code to show it

print(json_encode($output, JSON_UNESCAPED_UNICODE));

However, I am getting the error Subquery returns more than 1 row.

this is tbl_peoplesms

and tbl_people_comment

Answer Source

Instead of using an inline subquery, just left join tbl_people_comment on tbl_peoplesms and move the b.visible=1 condition into the join clause:

SELECT, count(b.text) as ccomment
FROM tbl_peoplesms AS c
LEFT JOIN tbl_people_comment AS b ON b.idsms= and b.visible=1

If you would like to include more fields from the tbl_people_comment table in the select list, then add them to the group by clause as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download