|  | 
	                
  
    | 
	 
        UNION หรือinner join ตารางปัจจุบันกับตารางเก่า ช่วยแนะนำหน่อยครับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | ตาราง merge คือตารางเก็บข้อมูลปัจจุบัน   merge_rep ตารางเก็บข้อมูลเก่า    ผมอยากดึงข้อมูลมาแสดงรวมกันแต่ต้องอยุ่ในเงื่อนไข 
 Code (PHP)
 
 <?
session_start();
require "../../include/config.inc.php";
require "../../include/functions.inc.php";
require "../../include/db_connect.inc.php";
require "../../include/lang-handler.inc.php";
require "../../include/wordings.inc.php";
if (empty($pid))
	$pid = 0;
$sql = "select date_format(route_date,'%e-%b-%Y'),route_time from routes where route_id = $route_id";
$rs = mysql_query($sql);
list($sel_date,$sel_time) = mysql_fetch_array($rs);
$color1 = "#efefef";
$color2 = "#dfdfdf";
$sumqty = 0;
$sumprice = 0;
$pageSize = $vip_page_size;
if (empty($page))
	$page = 1;
$sql = "select count(*) from merge where check_out = 1 and route = 2 and route_id = $route_id
		UNION
		select count(*) from merge_rep  where check_out = 1 and route = 2 and route_id = $route_id";
//$sql = "select count(*) from merge  where check_out = 1 and route = 2 and route_id = $route_id"; (เดิม)
$rs = mysql_query($sql);
list($noVehicles) = mysql_fetch_array($rs);
echo $sql."<br>";
$totalPage = getTotalPage($noVehicles,$pageSize);
$goto = ($page-1)*$pageSize;
$url = "check.php?rand=";
?>
<html>
<head>
<title><?=$domain_title ?></title>
<link rel='stylesheet' href='../../styles/admin.css'>
</head>
<body>
<? require "../../include/header.inc.php" ?>
<table align=center>
<tr>
	<td align=center><br><a href=select-route.php?pid=<?=$pid?>&trace=<?=$trace?>&lang=<?=$lang?>>กลับไปหน้าเลือกเที่ยวเวลา</a></td>
</tr>
</table>
<table align=center>
<tr>
	<td align=left><b><font color=#FF8A14>วันที่เดินทาง :</td>
	<? 	print ("<td align=left><b>$sel_date</td>"); ?>
</tr>
</table>
<table align=center>
<tr>
	<td align=left><b><font color=#FF8A14>เที่ยวเวลา :</td>
	<? 	print ("<td align=left><b>$sel_time</td>"); ?>
</tr>
</table>
<p>
<form name=sel action=print-detail-check.php TARGET='PrintTicketBook' method=get>
<table width=100%>
<tr>
	<td><input type=submit name=command value="Print" class=box> </td>
</tr>
</table>
<table cellpadding=0 cellspacing=1 width=100%>
<tr>
		<th width=4% class=thbar>ลำดับ</th>
		<th width=10% class=thbar>เลขที่บัตร</th>
		<th width=10% class=thbar>เลขที่ใบเสร็จ</th>
		<th width=40% class=thbar>รายการ</th>
		<th width=10% class=thbar>ทะเบียนรถ</th>
		<th width=10% class=thbar>จำนวน</th>
		<th width=16% class=thbar>จำนวนเงิน</th>
</tr>
<?
$bgcolor = $color1;
$sql = "select a.merge_id,a.book_id,a.queue_id,a.card_id,a.rec_no,a.qty,a.price,a.vehicle_id,b.vehicle_name ,a.vehicle_size,a.vehicle_regist,date_format(a.checkin_time,'%e-%b-%Y: %T'),date_format(a.checkout_time,'%e-%b-%Y: %T'),a.check_staff from merge a,vehicles b,routes c where a.check_out = 1 and a.remain = 0 and a.board_id = $route_id and a.vehicle_id = b.vehicle_id and a.route_id = c.route_id order by a.rec_no limit $goto,$pageSize
		UNION
		select a.merge_id,a.book_id,a.queue_id,a.card_id,a.rec_no,a.qty,a.price,a.vehicle_id,b.vehicle_name ,a.vehicle_size,a.vehicle_regist,date_format(a.checkin_time,'%e-%b-%Y: %T'),date_format(a.checkout_time,'%e-%b-%Y: %T'),a.check_staff from merge_rep a,vehicles b,routes c where a.check_out = 1 and a.remain = 0 and a.board_id = $route_id and a.vehicle_id = b.vehicle_id and a.route_id = c.route_id order by a.rec_no limit $goto,$pageSize
";
$rs = mysql_query($sql);
echo $rs."<br>";
$n = 0;
while (list($merge_id,$book_id,$queue_id,$card_id,$rec_no,$qty,$price,$vehicle_id,$vehicle_name ,$vehicle_size,$vehicle_regist,$checkin_time,$checkout_time,$check_staff) = mysql_fetch_array($rs))
	{
	$qty_str = number_format($qty);
	$price_str = number_format($price);
	$registBgcolor = "#fcde39";
	$sizeBgcolor = "#fda4f8";
	
	$no = $n+1;
	print ("<tr bgcolor=$bgcolor>");
			print ("<td align=center class=smaller>$no</td>");
			print ("<td align=center class=smaller>$card_id</td>");
			print ("<td align=center class=smaller>$rec_no</td>");
			print ("<td class=smaller> $vehicle_name</td>");
			print ("<td align=center bgcolor=$registBgcolor>$vehicle_regist</td>");
			print ("<td align=center class=smaller>$qty_str</td>");
			print ("<td align=center class=smaller>$price_str</td>");
	print ("</tr>");
	if ($bgcolor == $color1)
		$bgcolor = $color2;
	else
		$bgcolor = $color1;
	$n++;
	$sumqty = $sumqty+$qty;
	$sumprice = $sumprice+$price;
	}
$sumqty_str = number_format($sumqty);
$sumprice_str = number_format($sumprice);
?>
</table>
<table cellpadding=0 cellspacing=1 width=100%>
<tr>
		<th width=74% class=thbar>รวม</th>
		<? print ("<th width=10% class=thbar>$sumqty_str</th>"); ?>
		<? print ("<th width=16% class=thbar>$sumprice_str</th>"); ?>
</tr>
</table>
<input type=hidden name=from value="<?=$from?>">
<input type=hidden name=to value="<?=$to?>">
<input type=hidden name=route_id value="<?=$route_id?>">
</form>
<? printPageNavigator($page,$totalPage,$url) ?>
<p>
<? require "../../include/footer.inc.php" ?>
</body>
</html>
<? mysql_close($conn); ?>
 
 
 เน้นตรงนี้ครับ
 
 Code (PHP)
 
 $sql = "select count(*) from merge where check_out = 1 and route = 2 and route_id = $route_id
		UNION
		select count(*) from merge_rep  where check_out = 1 and route = 2 and route_id = $route_id";
//$sql = "select count(*) from merge  where check_out = 1 and route = 2 and route_id = $route_id"; (เดิม)
 
 กับ
 
 
 Code (PHP)
 
 $sql = "select a.merge_id,a.book_id,a.queue_id,a.card_id,a.rec_no,a.qty,a.price,a.vehicle_id,b.vehicle_name ,a.vehicle_size,a.vehicle_regist,date_format(a.checkin_time,'%e-%b-%Y: %T'),date_format(a.checkout_time,'%e-%b-%Y: %T'),a.check_staff from merge a,vehicles b,routes c where a.check_out = 1 and a.remain = 0 and a.board_id = $route_id and a.vehicle_id = b.vehicle_id and a.route_id = c.route_id order by a.rec_no limit $goto,$pageSize
		UNION
		select a.merge_id,a.book_id,a.queue_id,a.card_id,a.rec_no,a.qty,a.price,a.vehicle_id,b.vehicle_name ,a.vehicle_size,a.vehicle_regist,date_format(a.checkin_time,'%e-%b-%Y: %T'),date_format(a.checkout_time,'%e-%b-%Y: %T'),a.check_staff from merge_rep a,vehicles b,routes c where a.check_out = 1 and a.remain = 0 and a.board_id = $route_id and a.vehicle_id = b.vehicle_id and a.route_id = c.route_id order by a.rec_no limit $goto,$pageSize
 ";
 
 ช่วยดูให้หน่อยครับควรทำแบบไหนขอบคุณครับ
 
 
 
 Tag : PHP, MySQL
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2012-01-15 14:47:07 | By :
                          มือใหม่หัดเขียน | View :
                          1811 | Reply :
                          12 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอดูเบสหน่อย เงื่อนมีไรมั่ง
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-15 19:35:03 | By :
                            ozma |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เงื่อนไขก็ where a.check_out = 1 and a.remain = 0 and a.board_id = $route_id and a.vehicle_id = b.vehicle_id and a.route_id = c.route_id 
 
  
 ขอดูแบบนี้ไหมครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 09:19:45 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ดันๆๆๆๆ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 15:00:28 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ต้องการข้อมูลของสองตารางทั้งหมดใช่ไหมครับ 
 หรือตัดส่วนที่ซ้ำกันของสองตารางออกด้วย
 
 ถ้าเอาทั้งหมด หลัง UNION เพิ่ม ALL เข้าไปเป็น UNION ALL ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 16:12:49 | By :
                            amuropao08 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ครับ แต่ยังไม่ผ่านตั้งแต่เริ่มเลยครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 16:38:26 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ตั้งแต่เริ่มนี่ตรงไหนครับ มี Error หรือค่าไม่ออกยังไงบ้างครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 16:45:03 | By :
                            amuropao08 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ผมแก้ได้แบบนี้แต่ยังผิดอยุ มันerror 116
 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\............\check.php on line 116
 
 
 Code (PHP)
 
 $sql = "SELECT t.* FROM (
SELECT a.merge_id, a.book_id, a.queue_id, a.card_id, a.rec_no, a.qty, a.price, a.vehicle_id, a.vehicle_size, a.vehicle_regist, DATE_FORMAT( a.checkin_time, '%e-%b-%Y: %T' ) , DATE_FORMAT( a.checkout_time, '%e-%b-%Y: %T' ) , a.check_staff
FROM merge_samui a, vehicles b, routes c
WHERE a.check_out =1
AND a.remain =0
AND a.board_id = $route_id
AND a.vehicle_id = b.vehicle_id
AND a.route_id = c.route_id
UNION
SELECT a.merge_id, a.book_id, a.queue_id, a.card_id, a.rec_no, a.qty, a.price, a.vehicle_id, a.vehicle_size, a.vehicle_regist, DATE_FORMAT( a.checkin_time, '%e-%b-%Y: %T' ) , DATE_FORMAT( a.checkout_time, '%e-%b-%Y: %T' ) , a.check_staff
FROM merge_samui_rep a, vehicles b, routes c
WHERE a.check_out =1
AND a.remain =0
AND a.board_id = $route_id
AND a.vehicle_id = b.vehicle_id
AND a.route_id = c.route_id
) t ORDER BY a.rec_no
LIMIT $goto, $pageSize";
$rs = mysql_query($sql);
echo $rs."<br>";
$n = 0;
while
(แถวตรงนี้116) ผิดตรงนี้ครับ
(list($merge_id,$book_id,$queue_id,$card_id,$rec_no,$qty,$price,$vehicle_id,$vehicle_name ,$vehicle_size,$vehicle_regist,$checkin_time,$checkout_time,$check_staff) = mysql_fetch_array($rs))
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 16:53:58 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลอง echo $sql แล้วเอาไปรันใน phpMyAdmin ดูครับ error น่าจะชัดเจนกว่า 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 17:02:39 | By :
                            amuropao08 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (PHP) 
 ผิดพลาด
คำค้น SQL: เอกสารอ้างอิง
SELECT t . *
FROM (
SELECT a.merge_id, a.book_id, a.queue_id, a.card_id, a.rec_no, a.qty, a.price, a.vehicle_id, a.vehicle_size, a.vehicle_regist, DATE_FORMAT( a.checkin_time, '%e-%b-%Y: %T' ) , DATE_FORMAT( a.checkout_time, '%e-%b-%Y: %T' ) , a.check_staff
FROM merge_samui a, vehicles b, routes c
WHERE a.check_out =1
AND a.remain =0
AND a.board_id =40511
AND a.vehicle_id = b.vehicle_id
AND a.route_id = c.route_id
UNION SELECT a.merge_id, a.book_id, a.queue_id, a.card_id, a.rec_no, a.qty, a.price, a.vehicle_id, a.vehicle_size, a.vehicle_regist, DATE_FORMAT( a.checkin_time, '%e-%b-%Y: %T' ) , DATE_FORMAT( a.checkout_time, '%e-%b-%Y: %T' ) , a.check_staff
FROM merge_samui_rep a, vehicles b, routes c
WHERE a.check_out =1
AND a.remain =0
AND a.board_id =40511
AND a.vehicle_id = b.vehicle_id
AND a.route_id = c.route_id
)t
ORDER BY a.rec_no
LIMIT 0 , 10000
MySQL แสดง: เอกสารอ้างอิง
#1054 - Unknown column 'a.rec_no' in 'order clause'
 
 
 ไว้ ORDER BY a.rec_no ตรงไหนครับให้ถูดต้อง
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 17:14:46 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองเปลี่ยนเป็น rec_no หรือ t.rec_no error บอกว่าไม่รู้จักชื่อคอลัมน์นี้น่ะครับ
 เพราะคุณเอาข้อมูลที่ได้มาลงตาราง t ด้วย
 จริงๆ ไม่ต้องก็ได้ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 17:21:55 | By :
                            amuropao08 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ได้แล้วครับขอบคุณมากครับ       
 Code (PHP)
 
 $sql = "SELECT t.* FROM (
SELECT a.merge_id, a.book_id, a.queue_id, a.card_id, a.rec_no, a.qty, a.price, a.vehicle_id, b.vehicle_name, a.vehicle_size, a.vehicle_regist, DATE_FORMAT( a.checkin_time, '%e-%b-%Y: %T' ) , DATE_FORMAT( a.checkout_time, '%e-%b-%Y: %T' ) , a.check_staff
FROM merge_samui a, vehicles b, routes c
WHERE a.check_out =1
AND a.remain =0
AND a.board_id = $route_id
AND a.vehicle_id = b.vehicle_id
AND a.route_id = c.route_id
UNION 
SELECT a.merge_id, a.book_id, a.queue_id, a.card_id, a.rec_no, a.qty, a.price, a.vehicle_id, b.vehicle_name, a.vehicle_size, a.vehicle_regist, DATE_FORMAT( a.checkin_time, '%e-%b-%Y: %T' ) , DATE_FORMAT( a.checkout_time, '%e-%b-%Y: %T' ) , a.check_staff
FROM merge_samui_rep a, vehicles b, routes c
WHERE a.check_out =1
AND a.remain = 0
AND a.board_id = $route_id
AND a.vehicle_id = b.vehicle_id
AND a.route_id = c.route_id
) t ORDER BY t.rec_no
LIMIT $goto, $pageSize";
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 17:25:33 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ครับไม่ต้องใส่ตาราง t ก็ได้   
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2012-01-17 17:33:17 | By :
                            AOT |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |