Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > UNION หรือinner join ตารางปัจจุบันกับตารางเก่า ช่วยแนะนำหน่อยครับ



 

UNION หรือinner join ตารางปัจจุบันกับตารางเก่า ช่วยแนะนำหน่อยครับ

 



Topic : 071994

Guest




ตาราง 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>&nbsp;</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>&nbsp;$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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-01-15 14:47:07 By : มือใหม่หัดเขียน View : 1691 Reply : 12
 

 

No. 1



โพสกระทู้ ( 347 )
บทความ ( 0 )



สถานะออฟไลน์


ขอดูเบสหน่อย
เงื่อนมีไรมั่ง






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-15 19:35:03 By : ozma
 


 

No. 2

Guest


เงื่อนไขก็ 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

ตัวอย่าง my sql

ขอดูแบบนี้ไหมครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 09:19:45 By : AOT
 

 

No. 3

Guest


ดันๆๆๆๆ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 15:00:28 By : AOT
 


 

No. 4



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


ต้องการข้อมูลของสองตารางทั้งหมดใช่ไหมครับ

หรือตัดส่วนที่ซ้ำกันของสองตารางออกด้วย

ถ้าเอาทั้งหมด หลัง UNION เพิ่ม ALL เข้าไปเป็น UNION ALL ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 16:12:49 By : amuropao08
 


 

No. 5

Guest


ครับ
แต่ยังไม่ผ่านตั้งแต่เริ่มเลยครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 16:38:26 By : AOT
 


 

No. 6



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


ตั้งแต่เริ่มนี่ตรงไหนครับ
มี Error หรือค่าไม่ออกยังไงบ้างครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 16:45:03 By : amuropao08
 


 

No. 7

Guest


ผมแก้ได้แบบนี้แต่ยังผิดอยุ
มัน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
 


 

No. 8



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


ลอง echo $sql แล้วเอาไปรันใน phpMyAdmin ดูครับ error น่าจะชัดเจนกว่า
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 17:02:39 By : amuropao08
 


 

No. 9

Guest


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
 


 

No. 10



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


ลองเปลี่ยนเป็น rec_no หรือ t.rec_no
error บอกว่าไม่รู้จักชื่อคอลัมน์นี้น่ะครับ
เพราะคุณเอาข้อมูลที่ได้มาลงตาราง t ด้วย
จริงๆ ไม่ต้องก็ได้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 17:21:55 By : amuropao08
 


 

No. 11

Guest


ได้แล้วครับขอบคุณมากครับ

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
 


 

No. 12

Guest


ครับไม่ต้องใส่ตาราง t ก็ได้
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-17 17:33:17 By : AOT
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : UNION หรือinner join ตารางปัจจุบันกับตารางเก่า ช่วยแนะนำหน่อยครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่