簡體   English   中英

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

[英]SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause

我有一個關於在兩個表上加入WHERE子句的問題,它涉及monthname(date)='$ 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'";

您可以-

$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'";
<?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語句。 即使在兩個table2上都沒有匹配的dv_id,它仍將顯示記錄。 它仍然應該顯示記錄。。COLUMN ROLLS IF如果不匹配,它將顯示0.00值。 鏈接>> http://i599.photobucket.com/albums/tt79/emcevo/viewphpdisplay_zpsfc6a8174.jpg

您可以通過將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'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM