Rumah > Soal Jawab > teks badan
P粉4760461652023-08-14 10:06:04
Boleh guna case when
SELECT t2.item_id, t2.item_name, t2.menu_id, t1.menu_name, MAX(CASE WHEN t3.currency_code = 'EUR' THEN t3.price END) AS p_EUR, MAX(CASE WHEN t3.currency_code = 'USD' THEN t3.price END) AS p_USD, MAX(CASE WHEN t3.currency_code = 'GBP' THEN t3.price END) AS p_GBP, MAX(CASE WHEN t3.currency_code = 'RON' THEN t3.price END) AS p_RON, MAX(CASE WHEN t3.currency_code = 'LEV' THEN t3.price END) AS p_LEV FROM table_2 AS t2 JOIN table_1 AS t1 ON t2.menu_id = t1.menu_id LEFT JOIN table_3 AS t3 ON t2.item_id = t3.item_id GROUP BY t2.item_id, t2.item_name, t2.menu_id, t1.menu_name;
Jika anda perlu menukar null kepada 0, anda boleh menggunakan COALESCE
seperti yang ditunjukkan di bawah
COALESCE(MAX(CASE WHEN t3.currency_code = 'LEV' THEN t3.price END), 0) AS p_LEV