这个问题是对Is there a standard way to swap a query from an ID to a different column like 'name'?
此查询工作正常:
SELECT manufacturers.id, manufacturers.name, models.model_manufacturer, models.model_part
FROM models
INNER JOIN manufacturers
ON manufacturers.id = models.model_manufacturer
WHERE model_part = (
SELECT parts.id
FROM parts
WHERE parts.name = '{$part_name}')
GROUP BY manufacturers.id
ORDER BY name ASC
但是这个,当它遇到一个拥有多个模型的制造商时,失败并显示“子查询返回超过 1 行”:
SELECT parts.id, models.id, models.model_part, models.model_manufacturer, models.name, models.year, models.weight
FROM parts
INNER JOIN models
ON parts.id = models.model_part
WHERE model_manufacturer = (
SELECT manufacturers.id
FROM manufacturers
WHERE manufacturers.name = '{$manufacturer_name}')
&& model_part = (
SELECT parts.id
FROM parts
WHERE parts.name = '{$part_name}')
我不太了解的是,如果我只运行工作示例中的子查询,或者在第二个示例中单独运行子查询,它们都返回一个值。如果我运行任一整个查询,它们都会返回多个值。那么为什么一个错误而另一个没有呢?在 myPHPAdmin 中运行查询可以正常工作,但是当我尝试在我的 PHP 文件中执行此操作时失败。