insert age of account MYSQL 查询

insert age of account MYSQL query

找到 start_dateaccount_add_date 这两个日期的差异然后插入名为 account_age 的列的最佳方法是什么?

我需要的是用户 select start_date 说是一月。

然后他们添加的日期 account_add_date 说是三月。

相差 2 个月,所以 account_age 是 2,

然后在 4 月,2 将是 3。5 月 4 日,6 月 5 日,依此类推

有人知道怎么做吗?

我当前的插入查询MODEL

 function create_bank()
{
    $this->load->helper('date');

    $new_bank_acc_insert_data = array(
        'bank_name' => $this->input->post('bank_name'),
        'interest' => ($this->input->post('interest') / 100),
        'start_amount' => $this->input->post('start_amount'),
        'length' => $this->input->post('length'),
        'start_date' => date('Y-m-d',strtotime($this->input->post('start_date'))),
        'mem_id' => $this->session->userdata('id'),
        'account_add_date' => $this->current_date()
    );

    $insert = $this->db->insert('bank', $new_bank_acc_insert_data);
    return $insert;
}

关于查找帐户年龄的想法

SELECT DATEDIFF('start_date','account_add_date')
SELECT     
(
   12* (YEAR(account_add_date) - YEAR(start_date)) + 
          (MONTH(account_add_date) - MONTH(start_date))
) AS differenceInMonth
FROM
YOUR_TABLE

尽管 DATEDIFF 函数 returns 两个日期的天数差异比月数差异更准确

解释:

示例:

start_date = 2014 March

account_add_date = 2015 January

YEAR(account_add_date) = 2015

YEAR(start_date) = 2014

MONTH(account_add_date) = 1

MONTH(start_date) = 3

So according to the query:

12 * (2015-2014) + (1-3) = 12 * 1 - 2 = 10 (Months)