在 MySQL 中使用此查詢時出現錯誤。
這是查詢。
select course_id, sec_id, ID,
decode(name, NULL, '?', name)
from (section natural left outer join teaches)
natural left outer join instructor
where semester='Spring' and year=2010
這是錯誤錯誤代碼:1305.FUNCTION sql_univeristy decode does not exist
uj5u.com熱心網友回復:
是的,在 mySQL 中不存在 DECODE。有一個類似的 ELT 函式,但我認為不完全相同,而且您的用例也沒有真正需要它。COALESCE 可能是您想要的,但這里有四種不同的方法:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f9d37a049fc503b1b4848b7e278f2e34
create table test (name varchar(20));
insert into test(name) values('test'), ('jim'), (null);
select * from test;
SELECT name,
coalesce(name, '-') as method1,
if(name is null, '-', name) as method2,
case when name is null then '-' else name end as method3,
elt((name is null) 1, name, '-') as method4
FROM test
COALESCE 回傳第一個非空引數。如果不符合ANSI標準,但它允許有條件的邏輯,就像CASE做(和CASE是ANSI標準,這樣你就可以將它移植從一個RDBMS到另一個)。
最后是ELT。那只是回傳與提供的索引匹配的引數。因此,您可以使用 IS NULL 來測驗名稱,如果是,則回傳 1,否則將回傳 0。由于如果索引小于 1,則 ELT 回傳 null,因此您必須在 IS NULL 檢查中添加一個。這絕對沒有其他選項那么簡單,我不會僅僅為了做一個簡單的空檢查而經歷所有這些。如果這就是您想要做的所有事情,那么 COALESCE 就是您要走的路。
uj5u.com熱心網友回復:
Decodeoracle中存在,mysql不支持。相反,您可以使用:coalesce在您的情況下達到相同的效果;它回傳第一個非 NULL 引數:
select course_id, sec_id, ID,
coalesce(name, '-')
from (section natural left outer join teaches)
natural left outer join instructor
where semester='Spring' and year=2010
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393642.html
上一篇:從.txt檔案運行mySql命令
