2

我得到“错误:#1242 - 子查询返回超过 1 行”尝试执行此查询:

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

您对此查询有什么建议吗?

4

3 回答 3

3

JOIN这两个表而不是IN谓词,如下所示:

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);
于 2013-02-04T06:51:30.403 回答
1

检查此查询:

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

也许它的结果不止一排。

或者您可以限制子查询结果。

SELECT id FROM postcodes WHERE pcd LIKE (SELECT CONCAT(pcd,' %') FROM towns WHERE id IN (31898,12828,15771,7604) LIMIT 1)
于 2013-02-04T06:46:19.040 回答
0

您必须将子查询限制为一行。您应该添加一些过滤器以将结果限制为一行,或者添加LIMIT 1到您的子查询中将为您解决问题。

于 2013-02-04T06:46:45.500 回答