db表中的php值减去不同表中的多个行值

时间:2012-08-10 09:54:14

标签: php mysql

我正在努力弄清楚如何从table1获取原始entitlement数字,然后在开始日期顺序中从table2中减去每个请求的'days'。

第一张表获得他们的权利数字

第二张表获取请求

任何指导都将不胜感激,我正在寻找的输出是:

Start Date       End Date       Days to be taken        Days remaining

-                   -                   -                      22

01 Sep 2011      03 Sep 2011            3                      19
10 Sep 2011      11 Sep 2011            2                      17

这是我的代码:

$is_business_result = mysql_query('SELECT * FROM holiday_entitlement_business_manual WHERE employee = \'' . $username . '\' AND academic_year = \'' . $acyear . '\' ');

if($is_business = mysql_fetch_array($is_business_result)) {
echo '<div style="float:left; width:400px;">';
echo '<table width="100%">
<tr>
<td><strong>Name:</strong></td>
<td>'.$is_business['employee'].'</td>
</tr>
<tr>
<td><strong>Entitlement:</strong></td>
<td>'.$is_business['entitlement'].' '.$is_business['units'].'</td>
</tr>
<tr>
<td><strong>Department / Division:</strong></td>
<td>'.$is_business['division'].'</td>
</tr>
<tr>
<td><strong>Line Manager:</strong></td>
<td>'.$is_business['line_manager'].'</td>
</tr>
</table>';
echo '<br/>';
echo '<br/>';
echo '<br/>';    
echo '</div>';





echo '<table width="100%">
<tr>
<td><strong>Start Date</strong></td>
<td><strong>End Date</strong></td>
<td><strong>Days to be taken</strong></td>
<td><strong>Days remaining</strong></td>
</tr>';

echo '<tr>';

echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>'.$is_business['entitlement'].'</td>';

echo '</tr>';







}

$requests_result = mysql_query('SELECT * FROM requests WHERE employee = \'' . $username . '\' AND approved = 1 AND academic_year = \'' . $acyear . '\' ORDER BY start_date ASC');

while($requests = mysql_fetch_array($requests_result)) {

$start_date = new DateTime($requests['start_date']);
$end_date = new DateTime($requests['end_date']);

$timestamp_start_date = $start_date->getTimestamp();
$timestamp_end_date = $end_date->getTimestamp();

$formatted_start_date = date("d M Y", $timestamp_start_date);           
$formatted_end_date = date("d M Y", $timestamp_end_date);           

echo '<tr>';
echo '<td>'.$formatted_start_date.'</td>';
echo '<td>'.$formatted_end_date.'</td>';
echo '<td>'.$requests['days'].'</td>';
echo '</tr>';
}

echo'</table>';

TABLES

CREATE TABLE IF NOT EXISTS `requests` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `academic_year` varchar(255) NOT NULL,
  `employee` varchar(255) NOT NULL,
  `start_date` varchar(255) NOT NULL,
  `end_date` varchar(255) NOT NULL,
  `days` varchar(255) NOT NULL,
  `user` int(20) NOT NULL,
  `approved` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

CREATE TABLE IF NOT EXISTS `holiday_entitlement_business_manual` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `academic_year` varchar(255) NOT NULL,
  `employee` varchar(255) NOT NULL,
  `full_part_time` varchar(255) NOT NULL,
  `weekly_hours` varchar(255) NOT NULL,
  `division` varchar(255) NOT NULL,
  `date_of_commencement` varchar(255) NOT NULL,
  `entitlement` varchar(255) NOT NULL,
  `units` varchar(255) NOT NULL,
  `line_manager` varchar(255) NOT NULL,
  `length_of_service` varchar(255) NOT NULL,
  `band` varchar(255) NOT NULL,
  `new_entitlement` varchar(255) NOT NULL,
  `weekly_entitlement` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=227 ;

1 个答案:

答案 0 :(得分:0)

我猜员工是外键,如果是,那么你可以根据员工加入两个表,然后获取所有数据并从php中减去权利的天数