PDA

View Full Version : need experts help with complicated query


mwm
10-18-2007, 02:47 PM
I would like to know if I could do the 3 queries below in a single one. There has got to be a better way so the formating is across the screen and not down within the 3 tables. So I would think if 1 query could do the job then the formating would as well.

Thanks


<?
include "conn.php";
session_start();

$community=$_POST['community'];
$startdate=$_POST['startdate'];
$edate=$_POST['edate'];

?>
<html>
<body>
<title> Report</title>
<head>

</head>
<form action = "repprocess.php" method ="POST"></head>
<h1><p align=center>Report</p><p align=center> <?echo $community?></p></h1><h4><br>
Report Date <?echo $startdate?> - <?echo $edate?></h4>
<hr>

<?


$result =mysql_query ("SELECT customers.id, customers.community, csi.id, csi.contractor,count(contractor), csi.issuedate from customers,csi where customers.id = csi.id and csi.issuedate between '$startdate' and '$edate' and customers.community = '$community' group by csi.contractor");



echo "<table border=\"0\">";
echo "<th>Contractor</th>";
echo "<th>Issued</th>";

if (mysql_fetch_row($result) == 0)
{
echo "<center><font color = red>None!</font></center>";
}
else
{


$j = 0;

while ($row = mysql_fetch_array($result)) {




for ($i= 0; $i<mysql_fetch_row($row); $i ++) {

}

echo ($j++%2==0) ? '<tr bgcolor="#FFFFCC">' : '<tr bgcolor="#CCFF99">';

echo "<h4>";


echo "<td>" .($row[3]) . "</td>";
echo "<td>" .($row[4]) . "</td>";




}
echo '</table>';
echo "</center>";


}

?>

<?

$result1 =mysql_query ("SELECT customers.id, customers.community, csi.id, csi.contractor,count(contractor), csi.issuedate, csi.status='closed',count(status) from customers,csi where customers.id = csi.id and csi.issuedate between '$startdate' and '$edate' and customers.community = '$community' group by csi.contractor");





echo "<table border=\"0\">";
echo "<th>Contractor</th>";
echo "<th>Closed</th>";


if (mysql_fetch_row($result1) == 0)
{
echo "<center><font color = red>None!</font></center>";
}
else
{


$j = 0;

while ($row = mysql_fetch_array($result1)) {




for ($i= 0; $i<mysql_fetch_row($row); $i ++) {

}

echo ($j++%2==0) ? '<tr bgcolor="#FFFFCC">' : '<tr bgcolor="#CCFF99">';

echo "<h4>";


echo "<td>" .($row[3]) . "</td>";
echo "<td>" .($row[6]) . "</td>";



}
echo '</table>';
echo "</center>";


}

?>


<?

$result2 =mysql_query ("SELECT customers.id, customers.community, csi.id, csi.contractor, csi.status='open',count(status) from customers,csi where customers.id = csi.id and customers.community = '$community' AND status='open' group by csi.contractor");



echo "<table border=\"0\">";
echo "<th>Contractor</th>";
echo "<th>Open</th>";

if (mysql_fetch_row($result2) == 0)
{
echo "<center><font color = red>None!</font></center>";
}
else
{


$j = 0;

while ($row = mysql_fetch_array($result2)) {




for ($i= 0; $i<mysql_fetch_row($row); $i ++) {

}

echo ($j++%2==0) ? '<tr bgcolor="#FFFFCC">' : '<tr bgcolor="#CCFF99">';

echo "<h4>";


echo "<td>" .($row[3]) . "</td>";
echo "<td>" .($row[5]) . "</td>";



}
echo '</table>';
echo "</center>";


}

?>

<br>
<br>

<input type="button" value="MAIN" style="font-size:15" onClick="parent.location='managerstart.php'">


</form>
</body>
</html>

mic2100
10-18-2007, 05:09 PM
try this i think it may work for your purpose...



/*
SELECT
customers.id, customers.community, csi.id, csi.contractor,count(contractor), csi.issuedate, csi.status='closed',count(status)
FROM
customers,csi
WHERE (customers.id = csi.id AND csi.issuedate between '$startdate' AND '$edate' AND customers.community = '$community') OR
(customers.id = csi.id AND csi.issuedate between '$startdate' AND '$edate' AND customers.community = '$community') OR
(customers.id = csi.id AND customers.community = '$community' AND status='open')
GROUP BY
csi.contractor
*/

mwm
10-18-2007, 05:32 PM
Thanks for the suggestion. It worked some what. What I need to do is
1) Select all the contractors that were issued tickets between dates and count the number of tickets per contractor.
2) Then count the number of tickets per contractor that were "closed" during the same between dates
3) Then count per contractor all the remaining tickets that are still "open"

the column status is either open or closed.

Thanks for you help