我用一個簡單的存儲過程,努力從2個表收集數據併發送回PHP:mysql如何返回NULL值,如果沒有記錄
BEGIN
DECLARE cur_name VARCHAR(50);
SELECT currency_name INTO cur_name FROM currencies WHERE id = currency_id_var;
SELECT cur_name, sum(amount) AS income_amount, currency_id, month(income_date) AS month_of_income
FROM incomes
WHERE user_id = user_id_var
AND currency_id = currency_id_var
AND income_date > year_beginning_var
AND month(income_date) = month_var
GROUP BY currency_id;
END
它的工作好,除非是在'收入'表中找不到記錄。我需要使用'cur_name'設置(它始終有一些值)和其餘變量(income_amount,currency_id,month_of_income)設置爲NULL的過程的響應。 我在問一些無法做到的事情嗎? 我試圖使它與選擇IFNULL和UNION SELECT NULL種命令,沒有成功... 謝謝!
我不確定,但你應該檢查[this](https://stackoverflow.com/questions/28089275/select-statement-to-return-constant-when-no-records-found-in-table-in -sql-server#answer-28090639) –