5
我有MySQL查詢,如果子查詢返回沒有結果或空值,我需要在WHERE子句中替換值。MySQL檢查是否子查詢返回NULL,然後替換值
價格查詢其工作方式需要
成功運行
SELECT `prices` FROM `pricing`
WHERE (3 BETWEEN `from_unit` AND `to_unit`)
AND `type` = 1
AND `id_pricing` IN
(
SELECT v1.`id_pricing` FROM `values` AS v1
INNER JOIN `values` AS v2 ON v1.`id_pricing` = v2.`id_pricing`
INNER JOIN `values` AS v3 ON v1.`id_pricing` = v3.`id_pricing`
INNER JOIN `values` AS v4 ON v1.`id_pricing` = v4.`id_pricing`
WHERE v1.`id_attribute` = 1 AND v1.`id_value` = 1
AND v2.`id_attribute` = 7 AND v2.`id_value` = 63
AND v3.`id_attribute` = 8 AND v3.`id_value` = 87
AND v4.`id_attribute` = 12 AND v4.`id_value` = 143
)
當我如下修改此查詢,在IN子句中的子查詢添加IFNULL檢查,它會引發錯誤
'SQL錯誤(1242):子查詢返回多於一行'
SELECT `prices` FROM `pricing`
WHERE (3 BETWEEN `from_unit` AND `to_unit`)
AND `type` = 1
AND `id_pricing` IN
( IFNULL (
( SELECT v1.`id_pricing` FROM `values` AS v1
INNER JOIN `values` AS v2 ON v1.`id_pricing` = v2.`id_pricing`
INNER JOIN `values` AS v3 ON v1.`id_pricing` = v3.`id_pricing`
INNER JOIN `values` AS v4 ON v1.`id_pricing` = v4.`id_pricing`
WHERE v1.`id_attribute` = 1 AND v1.`id_value` = 1
AND v2.`id_attribute` = 7 AND v2.`id_value` = 63
AND v3.`id_attribute` = 8 AND v3.`id_value` = 87
AND v4.`id_attribute` = 12 AND v4.`id_value` = 143
),
'1234'
)
)
我試着用COALESCE替換IFNULL的結果仍然相同。我使用錯誤的語法嗎?
不'的'爲什麼SELECT IFNULL(v1.id_pricing, '1234')子查詢? –
下面是一個解決方案的類似問題:http://stackoverflow.com/questions/9861171/how-to-resolve-this-in-mysql-1242-subquery-returns-more-than-1-row – Gimmy
我'被這個子查詢混淆了。它應該做什麼?考慮通過演示方式提供sqlfiddle – Strawberry