简单的问题,但不知道如何解决。 如果 SELECT 部分不返回任何内容,则以下查询应返回“无值”。我会收到错误消息:
[21000][1241] (conn=80188403) Operand should contain 1 column(s)
这个查询有什么问题?
SET @produktNummer = 01000051311;
SET @languageID = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
SELECT IFNULL(
(SELECT product.id, product_number, property_group_translation.name AS AttributBez
FROM product
LEFT JOIN product_property
ON product.id = product_property.product_id
LEFT JOIN property_group_option
ON product_property.property_group_option_id = property_group_option.id
LEFT JOIN property_group_translation
ON property_group_option.property_group_id = property_group_translation.property_group_id
WHERE product_number = @produktNummer
AND property_group_translation.language_id = UNHEX(@languageID)
AND (
property_group_translation.name = '' OR
property_group_translation.name IS NULL
)
)
,'No values');
如果我尝试不使用 ISNULL(),我将不会得到任何结果。如果我像这样改变查询,我会得到多条记录
SET @produktNummer = 01000051311;
SET @languageID = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
SELECT product.id, product_number, property_group_translation.name AS AttributBez
FROM product
LEFT JOIN product_property
ON product.id = product_property.product_id
LEFT JOIN property_group_option
ON product_property.property_group_option_id = property_group_option.id
LEFT JOIN property_group_translation
ON property_group_option.property_group_id = property_group_translation.property_group_id
WHERE product_number = @produktNummer
AND property_group_translation.language_id = UNHEX(@languageID)
您可以获得至少 1 行包含数据或“NoValue”的行。
包含其他侧参数作为子查询并将其他数据加入其中。
(
select @produktNummer as produktNummer, UNHEX(@languageID) as languageID
) request
参见示例
SET @produktNummer = 01000051311;
SET @languageID = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
SELECT produktNummer,languageID
,coalesce(product.id,'NoValue') id
, coalesce(product_number,'NoValue')product_number
, coalesce(property_group_translation.name,'NoValue') AS AttributBez
FROM (
select @produktNummer as produktNummer, UNHEX(@languageID) as languageID
) request
left join product p on p.product_number = request.produktNummer
LEFT JOIN product_property
ON product.id = product_property.product_id
LEFT JOIN property_group_option
ON product_property.property_group_option_id = property_group_option.id
LEFT JOIN property_group_translation
ON property_group_option.property_group_id = property_group_translation.property_group_id
WHERE property_group_translation.language_id = request.languageID