0
我的任務是編輯我們的MySQL視圖之一,以包含缺失的行。問題是之前的視圖查詢使用的是if()語句,因爲某些原因我們沒有捕獲所有錶行。我絕不是一個SQL主並不能算出這個...MySQL查看邏輯與案例
原始代碼示例:
CREATE VIEW `b2b_data` AS SELECT
`asteriskcdrdb`.`cdr`.`uniqueid` AS `callid`,
`asteriskcdrdb`.`cdr`.`calldate` AS `ENTER`,
if((`asteriskcdrdb`.`cdr`.`dcontext` = 'from-internal'),`asteriskcdrdb`.`cdr`.`did`,`asteriskcdrdb`.`cdr`.`src`) AS `caller_id`,
if((`asteriskcdrdb`.`cdr`.`dcontext` = 'from-internal'),'outbound','inbound') AS `direction`,
if((`asteriskcdrdb`.`cdr`.`dcontext` = 'from-internal'),`asteriskcdrdb`.`cdr`.`src`,substr(`asteriskcdrdb`.`cdr`.`dstchannel`,5,3)) AS `AGENT`,
sec_to_time(`asteriskcdrdb`.`cdr`.`duration`) AS `AGENT_TIME`,
`asteriskcdrdb`.`cdr`.`disposition` AS `DISPOSITION`,
if((`asteriskcdrdb`.`cdr`.`dcontext` = 'from-internal'),`asteriskcdrdb`.`cdr`.`dstchannel`,`asteriskcdrdb`.`cdr`.`did`) AS `ROUTE`,
`asteriskcdrdb`.`cdr`.`recordingfile` AS `RECORDING` FROM `asteriskcdrdb`.`cdr` WHERE (((`asteriskcdrdb`.`cdr`.`dst` = '601') AND (`asteriskcdrdb`.`cdr`.`dcontext` = 'ext-group')) OR ((`asteriskcdrdb`.`cdr`.`dst` = '601') AND (`asteriskcdrdb`.`cdr`.`dcontext` = 'from-internal-xfer')) OR ((`asteriskcdrdb`.`cdr`.`dcontext` = 'from-internal') AND (`asteriskcdrdb`.`cdr`.`src` in ('143','146','317','318','320','225','214'))))
我看網上說IF ... ELSEIF只適用於存儲過程和函數。所以我放棄了這個想法。然後我想也許我可以使用CASE使查詢返回我需要的。毋庸置疑,這不起作用,顯然是錯誤的......但它確實顯示了我正在嘗試使用的邏輯。
改裝成代碼示例:
CREATE VIEW `b2b_data` AS SELECT
`asteriskcdrdb`.`cdr`.`uniqueid` AS `callid`,
`asteriskcdrdb`.`cdr`.`calldate` AS `ENTER`,
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN `asteriskcdrdb`.`cdr`.`did` AS `caller_id`;
WHEN 'from-internal-xfer' THEN `asteriskcdrdb`.`cdr`.`src` AS `caller_id`;
WHEN 'ext-local' THEN `asteriskcdrdb`.`cdr`.`src` AS `caller_id`;
WHEN 'ext-group' THEN `asteriskcdrdb`.`cdr`.`src` AS `caller_id`;
WHEN 'from-did-direct' THEN `asteriskcdrdb`.`cdr`.`src` AS `caller_id`;
END CASE;
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN 'outbound' AS `direction`;
WHEN 'from-internal-xfer' THEN 'inbound' AS `direction`;
WHEN 'ext-local' THEN 'inbound' AS `direction`;
WHEN 'ext-group' THEN 'inbound' AS `direction`;
WHEN 'from-did-direct' THEN 'inbound' AS `direction`;
END CASE;
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN `asteriskcdrdb`.`cdr`.`src` AS `AGENT`;
WHEN 'from-internal-xfer' THEN `asteriskcdrdb`.`cdr`.`dst` AS `AGENT`;
WHEN 'ext-local' THEN `asteriskcdrdb`.`cdr`.`dst` AS `AGENT`;
WHEN 'ext-group' THEN substr(`asteriskcdrdb`.`cdr`.`dstchannel`;5;3)) AS `AGENT`;
WHEN 'from-did-direct' THEN `asteriskcdrdb`.`cdr`.`dst` AS `AGENT`;
END CASE;
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN sec_to_time(`asteriskcdrdb`.`cdr`.`duration`) AS `AGENT_TIME`;
WHEN 'from-internal-xfer' THEN sec_to_time(`asteriskcdrdb`.`cdr`.`duration`) AS `AGENT_TIME`;
WHEN 'ext-local' THEN sec_to_time(`asteriskcdrdb`.`cdr`.`duration`) AS `AGENT_TIME`;
WHEN 'ext-group' THEN sec_to_time(`asteriskcdrdb`.`cdr`.`duration`) AS `AGENT_TIME`;
WHEN 'from-did-direct' THEN sec_to_time(`asteriskcdrdb`.`cdr`.`duration`) AS `AGENT_TIME`;
END CASE;
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN `asteriskcdrdb`.`cdr`.`disposition` AS `DISPOSITION`;
WHEN 'from-internal-xfer' THEN `asteriskcdrdb`.`cdr`.`disposition` AS `DISPOSITION`;
WHEN 'ext-local' THEN `asteriskcdrdb`.`cdr`.`disposition` AS `DISPOSITION`;
WHEN 'ext-group' THEN `asteriskcdrdb`.`cdr`.`disposition` AS `DISPOSITION`;
WHEN 'from-did-direct' THEN `asteriskcdrdb`.`cdr`.`disposition` AS `DISPOSITION`;
END CASE;
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN `asteriskcdrdb`.`cdr`.`dstchannel` AS `ROUTE`;
WHEN 'from-internal-xfer' THEN `asteriskcdrdb`.`cdr`.`dstchannel` AS `ROUTE`;
WHEN 'ext-local' THEN `asteriskcdrdb`.`cdr`.`did` AS `ROUTE`;
WHEN 'ext-group' THEN `asteriskcdrdb`.`cdr`.`did` AS `ROUTE`;
WHEN 'from-did-direct' THEN `asteriskcdrdb`.`cdr`.`did` AS `ROUTE`;
END CASE;
CASE `asteriskcdrdb`.`cdr`.`dcontext`
WHEN 'from-internal' THEN `asteriskcdrdb`.`cdr`.`recordingfile` AS `RECORDING` FROM `asteriskcdrdb`.`cdr` WHERE `asteriskcdrdb`.`cdr`.`src` IN ('143';'146';'317';'318';'320';'225';'214');
WHEN 'from-internal-xfer' THEN `asteriskcdrdb`.`cdr`.`recordingfile` AS `RECORDING` FROM `asteriskcdrdb`.`cdr` WHERE `asteriskcdrdb`.`cdr`.`dst` IN ('143';'146';'317';'318';'320';'225';'214');
WHEN 'ext-local' THEN `asteriskcdrdb`.`cdr`.`recordingfile` AS `RECORDING` FROM `asteriskcdrdb`.`cdr` WHERE `asteriskcdrdb`.`cdr`.`dst` IN ('143';'146';'317';'318';'320';'225';'214');
WHEN 'ext-group' THEN `asteriskcdrdb`.`cdr`.`recordingfile` AS `RECORDING` FROM `asteriskcdrdb`.`cdr` WHERE `asteriskcdrdb`.`cdr`.`dst` = '601';
WHEN 'from-did-direct' THEN `asteriskcdrdb`.`cdr`.`recordingfile` AS `RECORDING` FROM `asteriskcdrdb`.`cdr` WHERE `asteriskcdrdb`.`cdr`.`dst` IN ('143';'146';'317';'318';'320';'225';'214');
END CASE;
明天我會給你一個鏡頭,如果能正常工作,請提高你的答案。我有一種感覺,我錯誤地寫了三遍......我用逗號和分號分隔WHEN子句。以爲我不得不因爲MySQL文檔示例[這裏](https://dev.mysql.com/doc/refman/5.7/en/case.html)。 –
該文檔適用於在存儲過程中使用的'CASE' **語句**;分號用於它控制的語句的結尾。 [CASE **表達式**]在[此處]中有描述(http://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html) – Barmar
另請參見[案例表達與案例聲明] (http://stackoverflow.com/questions/12436859/case-expression-vs-case-statement) – Barmar