如何更新数据库的所有行上的特定列

问题描述 投票:-1回答:2

我必须在用户在上一个日期创建新条目时更新列opcash我必须更新prev日期opcash列,并且今天还要opcash列,其中opcash是总信用额的总和 - 总借记金额的总和。并且当用户输入信用栏时,必须添加opcash总额,并且当用户在借记栏上输入金额时必须减去该过程,该过程将持续所有日子。我的问题是opcash值是正确的如果用户在连续的一天创建一个条目,如果用户在25-12-2018,26-12-2018,27-12-2018创建条目,则用户继续正确的顺序。如果用户在18-12-2018创建一个条目,如果用户在25-12-2018创建一个条目,opcash值将在25-12-2018更新,但不会在26-12-2018更新。请帮我改变用户在前一天创建新条目时的opcash值

 public function insert()
 {
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];
    $dc=$this->input->post('dc');
$startdate = $this->input->post('TDate');
$date = str_replace('/', '-', $startdate);
$newDate = date("d/m/Y", strtotime($date));  
        if($dc=='c'){
        $amount=$this->input->post('credit1');
                    } 
        else if ($dc=='d') {
        $amount=$this->input->post('debit');
    }

$data=array(
  'date'=>$newDate,
  'code' =>$this->input->post('TName'),
  'project' =>$this->input->post('TName'),
  'part' =>$this->input->post('part1'),
  'part1' =>$this->input->post('part2'),
  'dc'=>$this->input->post('dc'),
  'amount'=>$amount,
   );
$this->db->insert('daybook',$data);
this->db->where('dc','c');
$this->db->select_sum('amount');
$total_credit= $this->db->get_where('daybook',array('date' => $newDate))- 
>row_array();
$this->db->where('dc','d');
$this->db->select_sum('amount');
$total_debit= $this->db->get_where('daybook',array('date' => $newDate))- 
>row_array();
$prev_date = date('d/m/Y', strtotime('-1 day'));
$result2 = $this->db->query("SELECT opcash FROM daytot WHERE 
tdate='$newDate'")->row_array();
$dc=$this->input->post('dc');
$total_credit1=0;
$total_debit1=0;
$opcash=0;
if($dc=='d')
{
 $amount=$this->input->post('debit');
 $opcash=$result2['opcash']-$amount;
}
if($dc=='c')
{
$amount=$this->input->post('credit1');
$opcash=$result2['opcash']+$amount;
}

 $data1 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>$total_debit['amount'],
'opcash'=>$opcash,
 );
 $data2 = array(
'tdate'=>$newDate,
'total_credit'=>$total_credit['amount'],
'total_debit'=>'0',
'opcash'=>$this->input->post('amount')+$result2['opcash'],
 );
 $data3 = array(
'tdate'=>$newDate,
'total_credit'=>'0',
'total_debit'=>$total_debit['amount'],
'opcash'=>$result2['opcash']-$this->input->post('amount'),
);
    $startdate = $this->input->post('TDate');
$date = str_replace('/', '-', $startdate);
$newDate = date("d/m/Y", strtotime($date));
    $objQuery = $this->db->get_where('daytot', array('tdate' => $newDate));
   if ($objQuery->num_rows() > 0) { //UPDATE
$this->db->where('tdate', $newDate);
$this->db->update('daytot', $data1);
 }
else{
$dc=$this->input->post('dc');
if ($dc=='c') {
$this->db->insert('daytot', $data2);
  }
if ($dc=='d') {
$this->db->insert('daytot', $data3);
}
}
$this->session->set_flashdata('Add', 'Inserted Successfully');
redirect('BookKeeping/daybook','refresh');
}

日记帐

--

    Table structure for table `daybook`
    --

    CREATE TABLE `daybook` (
      `recno` int(5) NOT NULL,
      `date` varchar(15) NOT NULL,
      `code` varchar(10) NOT NULL,
      `project` varchar(10) NOT NULL,
      `part` varchar(255) NOT NULL,
      `part1` varchar(255) NOT NULL,
      `amount` int(5) NOT NULL,
      `dc` varchar(5) NOT NULL,
      `ledreno` int(10) NOT NULL,
      `vouno` int(10) NOT NULL,
      `pvouno` varchar(100) NOT NULL,
      `type` varchar(100) NOT NULL,
      `mark` varchar(100) NOT NULL,
      `slno` varchar(100) NOT NULL,
      `shift` varchar(100) NOT NULL,
      `pacno` int(10) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `daybook`
    --

    INSERT INTO `daybook` (`recno`, `date`, `code`, `project`, `part`, `part1`, `amount`, `dc`, `ledreno`, `vouno`, `pvouno`, `type`, `mark`, `slno`, `shift`, `pacno`) VALUES
    (439, '24/12/2018', '1', '1', 'account', 'details', 500, 'c', 0, 0, '', '', '', '', '', 0),
    (440, '24/12/2018', '1', '1', 'praveen accounts', 'laavanya accounts', 500, 'c', 0, 0, '', '', '', '', '', 0),
    (441, '25/12/2018', '1', '1', 'praveen kumar accounts', 'praveen kumar accounts', 1000, 'c', 0, 0, '', '', '', '', '', 0),
    (442, '25/12/2018', '1', '1', 'abcdef', 'ghijklm', 100, 'd', 0, 0, '', '', '', '', '', 0),
    (443, '25/12/2018', '1', '1', 'xxxxxx', 'yyyyyyy', 100, 'c', 0, 0, '', '', '', '', '', 0),
    (444, '24/12/2018', '1', '1', 'abcdef', 'ghijklm', 1000, 'c', 0, 0, '', '', '', '', '', 0);

    --
    -- Indexes for dumped tables
    --

    --
    -- Indexes for table `daybook`
    --
    ALTER TABLE `daybook`
      ADD PRIMARY KEY (`recno`);

    --
    -- AUTO_INCREMENT for dumped tables
    --

    --
    -- AUTO_INCREMENT for table `daybook`
    --
    ALTER TABLE `daybook`
      MODIFY `recno` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=445;COMMIT;

    Daytot
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 26, 2018 at 07:37 AM
-- Server version: 10.1.25-MariaDB
-- PHP Version: 5.6.31

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `ci_fina`
--

-- --------------------------------------------------------

--
-- Table structure for table `daytot`
--

CREATE TABLE `daytot` (
  `tdate` varchar(15) NOT NULL,
  `id` int(11) NOT NULL,
  `total_credit` int(11) NOT NULL,
  `total_debit` int(11) NOT NULL,
  `opcash` int(11) NOT NULL,
  `clcash` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `daytot`
--

INSERT INTO `daytot` (`tdate`, `id`, `total_credit`, `total_debit`, `opcash`, `clcash`) VALUES
('24/12/2018', 76, 2000, 0, 2000, 0),
('25/12/2018', 77, 1100, 100, 2000, 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `daytot`
--
ALTER TABLE `daytot`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `daytot`
--
ALTER TABLE `daytot`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=78;COMMIT;

  [1]: https://i.stack.imgur.com/REU36.png
  [2]: https://i.stack.imgur.com/fVagp.png
php mysql codeigniter
2个回答
0
投票

尝试更改查询中的日期格式。通常表中的日期格式为Y-m-d:

$newDate = date("Y-m-d", strtotime($date));

0
投票

我创建了一个SQLFiddle来展示如何在不必维护该表的情况下获得与daytot表相同的结果。它还将日期列显示为DATE类型而不是VARCHAR。

通过在表中使用DATE类型而不是VARCHAR,可以防止将来出现几个可能的问题。它还允许您正确地按日期排序和选择,选择日期范围等。它还消除了与将行插入日历表时相关的任何问题,并处理对日记表的更新和删除。

SQL Fiddle

MySQL 5.6架构设置:

    CREATE TABLE `daybook` (
      `recno` int(5) NOT NULL,
      `date` DATE NOT NULL,
      `code` varchar(10) NOT NULL,
      `project` varchar(10) NOT NULL,
      `part` varchar(255) NOT NULL,
      `part1` varchar(255) NOT NULL,
      `amount` int(5) NOT NULL,
      `dc` varchar(5) NOT NULL,
      `ledreno` int(10) NOT NULL,
      `vouno` int(10) NOT NULL,
      `pvouno` varchar(100) NOT NULL,
      `type` varchar(100) NOT NULL,
      `mark` varchar(100) NOT NULL,
      `slno` varchar(100) NOT NULL,
      `shift` varchar(100) NOT NULL,
      `pacno` int(10) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `daybook`
    --

    INSERT INTO `daybook` (`recno`, `date`, `code`, `project`, `part`, `part1`, `amount`, `dc`, `ledreno`, `vouno`, `pvouno`, `type`, `mark`, `slno`, `shift`, `pacno`) VALUES
    (439, '2018/12/24', '1', '1', 'account', 'details', 500, 'c', 0, 0, '', '', '', '', '', 0),
    (440, '2018/12/24', '1', '1', 'praveen accounts', 'laavanya accounts', 500, 'c', 0, 0, '', '', '', '', '', 0),
    (441, '2018/12/25', '1', '1', 'praveen kumar accounts', 'praveen kumar accounts', 1000, 'c', 0, 0, '', '', '', '', '', 0),
    (442, '2018/12/25', '1', '1', 'abcdef', 'ghijklm', 100, 'd', 0, 0, '', '', '', '', '', 0),
    (443, '2018/12/25', '1', '1', 'xxxxxx', 'yyyyyyy', 100, 'c', 0, 0, '', '', '', '', '', 0),
    (444, '2018/12/24', '1', '1', 'abcdef', 'ghijklm', 1000, 'c', 0, 0, '', '', '', '', '', 0);

    --
    -- Indexes for dumped tables
    --

    --
    -- Indexes for table `daybook`
    --
    ALTER TABLE `daybook`
      ADD PRIMARY KEY (`recno`);

    --
    -- AUTO_INCREMENT for dumped tables
    --

    --
    -- AUTO_INCREMENT for table `daybook`
    --
    ALTER TABLE `daybook`
      MODIFY `recno` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=445;COMMIT;

-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 26, 2018 at 07:37 AM
-- Server version: 10.1.25-MariaDB
-- PHP Version: 5.6.31

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `ci_fina`
--

-- --------------------------------------------------------

--
-- Table structure for table `daytot`
--

CREATE TABLE `daytot` (
  `tdate` DATE NOT NULL,
  `id` int(11) NOT NULL,
  `total_credit` int(11) NOT NULL,
  `total_debit` int(11) NOT NULL,
  `opcash` int(11) NOT NULL,
  `clcash` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `daytot`
--

INSERT INTO `daytot` (`tdate`, `id`, `total_credit`, `total_debit`, `opcash`, `clcash`) VALUES
('2018/12/24', 76, 2000, 0, 2000, 0),
('2018/12/25', 77, 1100, 100, 2000, 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `daytot`
--
ALTER TABLE `daytot`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `daytot`
--
ALTER TABLE `daytot`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=78;COMMIT;

查询1:

SELECT 
  `date` as `tdate`,
  SUM(IF(`dc` = 'c',`amount`,0)) as `total_credit`,
  SUM(IF(`dc` = 'd',`amount`,0)) as `total_debit`,
  SUM(IF(`dc` = 'c',`amount`,0)) - SUM(IF(`dc` = 'd',`amount`,0)) as `opcash`,
  0 as `clcash`
FROM `daybook`
GROUP BY `date`

Results

|      tdate | total_credit | total_debit | opcash | clcash |
|------------|--------------|-------------|--------|--------|
| 2018-12-24 |         2000 |           0 |   2000 |      0 |
| 2018-12-25 |         1100 |         100 |   1000 |      0 |
© www.soinside.com 2019 - 2024. All rights reserved.