当用户在上一个日期创建新条目时,我必须更新opcash列,我必须更新上一个日期opcash列以及今天的opcash列,其中opcash是总贷方金额之和-总借方金额之和..并且,当用户在贷方列中输入时,该opcash必须与总贷方金额相加,而当用户在借方列中输入借方金额时,该opcash必须相减,该过程持续了整天。如果用户以正确的顺序进行交易,则正确。也就是说,如果用户在连续的一天中创建条目,即用户在25-12-2018,26-12-2018,27-12-2018上创建条目。如果用户在26-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');
}
Daybook
-
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
答案 0 :(得分:0)
尝试更改查询中的日期格式。通常表中的日期格式为Y-m-d:
$newDate = date("Y-m-d", strtotime($date));
答案 1 :(得分:0)
我创建了一个SQLFiddle,以显示如何在不维护该表的情况下获得与daytot表相同的结果。它还将日期列显示为DATE类型而不是VARCHAR。
通过在表中使用DATE类型而不是VARCHAR,可以防止将来出现多种问题。它还使您可以正确地按日期排序和选择,选择日期范围等。它还消除了与将行插入到daybook表中有关的任何问题,并处理了对daybook表的更新和删除。
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 |