带案例的MySQL View Logic

时间:2016-05-12 19:10:12

标签: mysql case sql-view

我的任务是编辑我们的一个MySQL视图,以包含它缺少的行。问题是之前的视图查询使用if()语句,由于某种原因我们没有捕获所有表行。我绝不是一个SQL大师,也无法解决这个问题......

原始代码示例:

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;

1 个答案:

答案 0 :(得分:0)

您将AS条款放在错误的位置。整个CASE表达式会生成一个值,您可以为其指定别名。并且您不在;子句之间或在案例之后使用WHEN,因为这会结束整个查询。 WHEN子句之间没有分隔符,整个表达式与SELECT的其他,列分开。

CASE `asteriskcdrdb`.`cdr`.`dcontext`
    WHEN 'from-internal' THEN `asteriskcdrdb`.`cdr`.`did`
    WHEN 'from-internal-xfer' THEN `asteriskcdrdb`.`cdr`.`src
    WHEN 'ext-local' THEN `asteriskcdrdb`.`cdr`.`src`
    WHEN 'ext-group' THEN `asteriskcdrdb`.`cdr`.`src`
    WHEN 'from-did-direct' THEN `asteriskcdrdb`.`cdr`.`src`
END CASE AS `caller_id`,

和其他CASE表达式相似。