Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL error: #1242 - Subquery returns more than 1 row

Tags:

sql

php

mysql

I got "error: #1242 - Subquery returns more than 1 row" trying to execute this Query:

SELECT id FROM postcodes WHERE pcd LIKE (SELECT CONCAT(pcd,' %') FROM towns WHERE id IN (31898,12828,15771,7604))

do you have any suggestion for this query?

like image 385
Amir Avatar asked Dec 30 '25 10:12

Amir


2 Answers

JOIN the two tables instead of the IN predicate, like this:

SELECT p.id 
FROM postcodes p
INNER JOIN towns t ON p.pcd LIKE CONCAT(t.pcd,' %')
WHERE t.id IN (31898,12828,15771,7604);
like image 183
Mahmoud Gamal Avatar answered Jan 01 '26 23:01

Mahmoud Gamal


Check this query :

SELECT CONCAT(pcd,' %') FROM towns WHERE id IN (31898,12828,15771,7604)

Maybe it's result is more than one row.

Or you can limit the subquery result.

SELECT id FROM postcodes WHERE pcd LIKE (SELECT CONCAT(pcd,' %') FROM towns WHERE id IN (31898,12828,15771,7604) LIMIT 1)
like image 34
Iswanto San Avatar answered Jan 01 '26 22:01

Iswanto San