SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句

时间:2014-05-02 13:24:00

标签: php mysql join inner-join

我在连接两个带有WHERE子句的表时遇到问题,该子句涉及月份名(日期)=' $ month' ..

**tbl_dv**
dv_id, number, net, respo, date_added 

**tbl_payroll**
pr_id, dv_id, number, pr_date, roll1, roll2, roll3, roll4, roll5, roll6 date_added.

我想将列ROLLS加起来并在view.php上查看。喜欢:$ total =(roll1 + roll2 + roll3 + roll4 + roll5 + roll6);如何做到并以独特的方式插入。

$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($data[0])."' && year(date_added)='$year' && month(date_added)='$month'";

3 个答案:

答案 0 :(得分:1)

你可以做 -

$viewrecord = "SELECT *, (roll1 + roll2 + roll3 + roll4 + roll5 + roll6) as total FROM tbl_dv join tbl_payroll on tbl_dv.dv_id = tbl_payroll.dv_id WHERE respo='".mysql_real_escape_string($data[0])."' && year(date_added)='$year' && month(date_added)='$month'";

答案 1 :(得分:1)

<?php require_once('../includes/connection.php');?>
<?php require_once('../includes/header.php');?>

<?php
$color="1";
$respo = $_GET['respo'];
$data = explode("+", $respo);
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";

**$viewrecord = "SELECT *, (pr.roll1 + pr.roll2 + pr.roll3 + pr.roll4 + pr.roll5 + pr.roll6) AS rolls FROM tbl_payroll dv join tbl_payroll pr on pr.dv_id = dv.dv_id WHERE dv.respo='".mysql_real_escape_string($data[0])."' && year(dv.date_added)='$year' && month(dv.date_added)='$month'";**
$run_viewrecord = mysql_query($viewrecord) or die(mysql_error());

{
echo "<table border='1' width='100%' style='border:1px solid silver' cellpadding='5px' cellspacing='0px'>
<tr bgcolor='#666666' style='color:#FFFFFF'>
<th>Date Encoded</th>
------------HEADER--------- etc....

-------THERE SHOULD BE A IF STATEMATE HERE-----------------
(where if no records match "dv.dv_id=pr.pr.dv_id". It would still display records from tbl_dv..)

while ($row = mysql_fetch_row($run_viewrecord)) {

if($color==1){
echo "<tr bgcolor='#ffffff'>";
echo "<td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
**--------- I WANT TO DISPLAY THE ROLLS HERE --------------------**
echo "</td></tr>";

$color="2";
}   else {
echo "<tr bgcolor='#ebeaea'>";
echo "<td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
**--------- I WANT TO DISPLAY THE ROLLS HERE --------------------**
echo "</td></tr>";

$color="1";
}
}
echo '</table>';
echo '<td><tr><table><br /><br />';
}
?>

我希望在WHILE之前添加一个IF语句。即使桌面2上没有匹配的dv_id,仍然会显示记录。它应该仍然显示记录.. COLUMN ROLLS如果不匹配它将显示0.00值。 LINK&GT;&GT; http://i599.photobucket.com/albums/tt79/emcevo/viewphpdisplay_zpsfc6a8174.jpg

答案 2 :(得分:0)

您可以通过在PHP中为每个记录行添加返回值来以编程方式执行此操作。

您也可以执行类似

的操作
 $viewrecord = "SELECT *, (pr.roll1 + pr.roll2 + 
                pr.roll3 + pr.roll4 + pr.roll5 + pr.roll6) as rolls 
 FROM tbl_dv dv LEFT OUTER JOIN tbl_payroll pr on dv.dv_id = pr.dv_id 
 RIGHT OUTER JOIN tbl_payroll pr on dv.dv_id = pr.dv_id 
 WHERE dv.respo='".mysql_real_escape_string($data[0])."' 
 && year(dv.date_added)='$year' && month(dv.date_added)='$month'";