我得到“错误:#1242 - 子查询返回超过 1 行”尝试执行此查询:
SELECT id FROM postcodes WHERE pcd LIKE (SELECT CONCAT(pcd,' %') FROM towns WHERE id IN (31898,12828,15771,7604))
您对此查询有什么建议吗?
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);
检查此查询:
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)
您必须将子查询限制为一行。您应该添加一些过滤器以将结果限制为一行,或者添加LIMIT 1
到您的子查询中将为您解决问题。