full_price
の計算をサブクエリで囲みます。
SELECT
MIN(full_price) AS min_full
, MAX(full_price) AS max_full
FROM
( SELECT IF( (SELECT COUNT(*) FROM test_table2 WHERE id = t.id) > 0
, t.price + (SELECT price FROM attribute WHERE id = t.id LIMIT 1)
, t.price
) AS full_price
FROM test_table AS t
WHERE ... --- if you want conditions applied to `test_table`
--- before calculating the `full_price`
) AS tmp ;
改善点:
- (標準SQL):
CASE
句を使用IF
します。これはMySQL専用ではありません。
- (パフォーマンス):をに変更
(SELECT COUNT ...) > 0
しEXISTS (SELECT ... )
ます。通常は高速です。
クエリは次のようになります。
SELECT
MIN(full_price) AS min_full
, MAX(full_price) AS max_full
FROM
( SELECT
CASE WHEN
EXISTS (SELECT * FROM test_table2 WHERE id = t.id)
THEN t.price + (SELECT price FROM attribute WHERE id = t.id LIMIT 1)
ELSE t.price
END AS full_price
FROM test_table AS t
WHERE ...
) AS tmp ;