写sql的老手~鸿池

今天再鸿池后面看着他写sql,啪啪啪打了一大串,这么长的一串sql要是我以前看起来根本就是不是我能企及的。可能你们看到我这样说觉得很可笑,个人能力不高,不过贵在努力学习

因为之前工作中涉及sql的操作并不多,说起来有些羞愧,这尼玛还好意思舔着脸和别人说工作经验xx年,工作项目xxxxxx。看看人家这写码的素质~,自愧不如啊~

首先,鸿池先在sql工具里写了第一段的sql作为a取出自己需要的字段检查正确性作为a,然后再在sql工具中写另一段sql取出自己需要的字段检查正确性作为b,然后再取出自己需要的字段。

废话不说上代码~~

select a.voucher_value, a.voucher_number as exp_voucher, date(a.issue_time) as exp_issue_date, a.expiry_date as exp_expiry_date
    , a.expiry_grace_date as exp_expiry_grace_date
    , b.voucher_number as new_voucher, date(a.replace_time) as replace_date
    , b.expiry_date as new_expiry_date
    , b.expiry_grace_date as new_expiry_grace_date
    from (
     select a.expired_voucher_id, b.voucher_value, concat(prefix, voucher_text, suffix) as voucher_number, 
     max(c.action_time) as issue_time, b.expiry_date
     , DATE_ADD(b.expiry_date, INTERVAL $this->_grace_period_days DAY) AS expiry_grace_date
     , a.replace_time
     from $this->_voucher_replace_details a, $this->_voucher b, $this->_voucher_action c
     where a.expired_voucher_id = b.voucher_id
     and b.voucher_id = c.voucher_id
     and c.action_id in ('$issue_action_id', '$issue_action_id2', '$issue_action_id3')
     $value_clause
     AND  (a.replace_time BETWEEN '$quarter_start_date 00:00:00' AND '$quarter_end_date 23:59:59' )
     group by b.voucher_id
    ) a,
    (
     select a.expired_voucher_id, b.voucher_value, concat(prefix, voucher_text, suffix) as voucher_number, 
     max(c.action_time) as issue_time, b.expiry_date
     , DATE_ADD(b.expiry_date, INTERVAL $this->_grace_period_days DAY) AS expiry_grace_date
     from $this->_voucher_replace_details a, $this->_voucher b, $this->_voucher_action c
     where a.new_voucher_id = b.voucher_id
     and b.voucher_id = c.voucher_id
     and c.action_id in ('$issue_action_id', '$issue_action_id2', '$issue_action_id3')
     AND  (a.replace_time BETWEEN '$quarter_start_date 00:00:00' AND '$quarter_end_date 23:59:59' )
     group by b.voucher_id
    ) b
    where a.expired_voucher_id = b.expired_voucher_id
    ORDER BY 2 ASC

留念学习,理清逻辑,其实你也可以做到~~