如果我設定會話變數(例如@tmp = 0),則可以根據是否滿足條件來運行/忽略查詢。
SET @create_table := IF(@tmp >= 1, 'CREATE TABLE table2 SELECT * FROM table1', 'no');
這可用于像上面的示例這樣的短查詢,但不適用于具有多個陳述句的更復雜的查詢。有可能做這樣的事情嗎?
IF(@tmp >= 1)
DO SOMETHING
ELSE
DO NOTHING
uj5u.com熱心網友回復:
正確的語法如下。
IF condition1 THEN
{...statements to execute when condition1 is TRUE...}
[ ELSEIF condition2 THEN
{...statements to execute when condition1 is FALSE and condition2 is TRUE...} ]
[ ELSE
{...statements to execute when both condition1 and condition2 are FALSE...} ]
END IF;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/356348.html
下一篇:清空剃須刀頁面檢查id是否存在
