1 Replies - 579 Views - Last Post: 11 March 2014 - 09:41 AM Rate Topic: -----

#1 rodneyqc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 02-August 13

Mysql Sum and Output per row

Posted 10 March 2014 - 02:49 PM

Hello Im Rhon, and i would like to ask how can i Join this values. Posted Image.
Lets say the bus number is the category. Bus No. 100001 which is provided in the image. How can i join the first and second table because they had the same bus no. My code separate a trip i.e bus no and tracking id. Here is the code.

the array values are the busNumbers in databse and tracking id so i can match them if equal. the problem is i.e i have 2 same busnumbers with different trip. The process is i select and get sum depending on busnum and tracking id if they match. But after the while loop for querying it will go back to for loop which will get the second number and the first and second are the same but diff tracking_id and then ofcourse it will output another table.. i dont know what solution should i do to join them together. Anyway here is my database table which is not that good but i want it to be solved using this table. Posted Image Sorry for my bad english. thank you
$db = mysqli_connect('localhost', 'root', '', 'capstone');

for($i = 0; $i<$countss; $i++) {
				$getbusnum = $arrBusNumber[$i];
				$gettrackid = $arrTracking_id[$i];
				$getfrom = $arrFrom[$i];
				$getto = $arrTo[$i];

				echo "
					<table id='summary_table' border='1' align='center'>
						<th colspan='5' align='center'> 	Bus Number: ".$getbusnum."</th>
							<th>Bus No</th>
							<th>No. of Passengers</th>

			$statement = " SELECT SUM(qty) as 'qty', SUM(total) as 'total', busNumber, origin, destination FROM reservation_lib WHERE reserved = 'true' AND date_trip = '$date' AND busNumber = '$getbusnum' AND tracking_id = '$gettrackid'";

			$sql = mysqli_query($db, $statement);

	if($hasData != 0)
		while($row = mysqli_fetch_array($sql))
			$qty = $row['qty'];
				$total = $row['total'];
			// $intval= intval(str_replace(",","",$total));
			// $total += $intval;
				echo "
						<tr align='center'>
					$sum += $total;
				//inner while
			} // while
		} // if
	}//for loop
	echo "</table>";

	if($hasData == 0)
		echo "<h1 align='center' style='color:green;'> No Records Found</h1>";

Is This A Good Question/Topic? 0
  • +

Replies To: Mysql Sum and Output per row

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2016
  • View blog
  • Posts: 6,142
  • Joined: 15-January 14

Re: Mysql Sum and Output per row

Posted 11 March 2014 - 09:41 AM

I don't know if I understand your specific question, but in general this is how you join 2 tables:

SELECT * FROM table1 INNER JOIN table2 ON table1.bus_number = table2.bus_number
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1