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 > วิธีดึงข้อมูลจาก 2 ตาราง...โดยที่ฟิลล์เหมือนกันทุกอย่าง แค่ชื่อ ตารางไม่เหมือนกัน



 

วิธีดึงข้อมูลจาก 2 ตาราง...โดยที่ฟิลล์เหมือนกันทุกอย่าง แค่ชื่อ ตารางไม่เหมือนกัน

 



Topic : 025798



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



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




วิธีดึงข้อมูลจาก 2 ตารางมาแสดงโดยที่ฟิลล์เหมือนกันทุกอย่าง แค่ชื่อ ตารางไม่เหมือนกัน



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-03-25 13:51:31 By : yutthana11 View : 2203 Reply : 10
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

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

ใช้คำสั่ง UNION ครับ

Code
SELECT Column1,Column2 FROM tb1
UNION
SELECT Column1,Column2 FROM tb2







Date : 2009-03-25 14:01:41 By : webmaster
 


 

No. 2



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



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


Code (PHP)
<?php
echo "เอาตารางมาดูว่ามันอยู่ยังไง";
?>

Date : 2009-03-25 14:11:11 By : pklangkua
 

 

No. 3



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



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


รวม 2 ตารางให้แสดงเป็นตารางเดียวครับ
Date : 2009-03-25 14:16:26 By : yutthana11
 


 

No. 4



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



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


ลองแล้วไม่ได้ error

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\AppServ\www\project\admin\hp.php on line 249


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>hp</title>
<style type="text/css">
<!--
body 
#Layer7 {
	position:absolute;
	width:766px;
	height:70px;
	z-index:2;
	left: 18px;
	top: 36px;
}
#Layer8 {
	position:absolute;
	width:445px;
	height:77px;
	z-index:3;
	left: 782px;
	top: 36px;
}
#Layer9 {	position:absolute;
	width:750px;
	height:1px;
	z-index:4;
	left: 156px;
	top: 203px;
	background-color: #CCCCCC;
}
#Layer10 {	position:absolute;
	width:1px;
	height:460px;
	z-index:5;
	left: 156px;
	top: 203px;
	background-color: #CCCCCC;
}
#Layer11 {	position:absolute;
	width:1px;
	height:460px;
	z-index:6;
	left: 906px;
	top: 203px;
	background-color: #CCCCCC;
}
#Layer15 {	position:absolute;
	width:751px;
	height:1px;
	z-index:8;
	left: 156px;
	background-color: #CCCCCC;
	top: 663px;
}
.style2 {	font-size: 16px;
	font-weight: bold;
	color: #0000FF;
}
#Layer1 {	position:absolute;
	width:467px;
	height:442px;
	z-index:1;
	left: 290px;
	top: 213px;
}
#Layer13 {	position:absolute;
	width:83px;
	height:21px;
	z-index:8;
	left: 82px;
	top: 294px;
}
#Layer16 {	position:absolute;
	width:76px;
	height:21px;
	z-index:9;
	left: 87px;
	top: 209px;
}
#Layer2 {	position:absolute;
	width:56px;
	height:21px;
	z-index:2;
	left: 97px;
	top: 40px;
}
#Layer3 {
	position:absolute;
	width:42px;
	height:21px;
	z-index:2;
	left: 429px;
	top: 181px;
}
#Layer4 {	position:absolute;
	width:32px;
	height:20px;
	z-index:2;
	left: 130px;
	top: 124px;
}
#Layer5 {	position:absolute;
	width:39px;
	height:22px;
	z-index:2;
	left: 123px;
}
#Layer6 {	position:absolute;
	width:43px;
	height:26px;
	z-index:2;
	left: 114px;
	top: 337px;
}
.style10 {font-size: 80%;
	font-family: "Courier New", Courier, monospace;
	font-weight: bold;
}
.style12 {font-size: 16px; font-family: "Courier New", Courier, monospace; }
.style13 {font-size: 80%; font-family: "Courier New", Courier, monospace; }
.style3 {font-family: "Courier New", Courier, monospace}
.style6 {font-size: 80%}
.style7 {font-size: 100%;
	font-family: "Courier New", Courier, monospace;
}
.style8 {color: #0000FF; font-size: 16px;}
#apDiv1 {
	position:absolute;
	width:659px;
	height:36px;
	z-index:9;
	left: 587px;
	top: 112px;
}
-->
</style>
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
#apDiv2 {
	position:absolute;
	width:591px;
	height:328px;
	z-index:10;
	left: 238px;
	top: 239px;
}
.style17 {font-weight: bold}
#apDiv3 {
	position:absolute;
	width:200px;
	height:77px;
	z-index:10;
	left: 176px;
	top: 36px;
}
#apDiv4 {
	position:absolute;
	width:51px;
	height:16px;
	z-index:10;
	left: 1194px;
	top: 249px;
}
.style24 {font-size: 14%}
.style32 {font-size: 68%}
#apDiv5 {
	position:absolute;
	width:118px;
	height:78px;
	z-index:11;
	left: 14px;
	top: 144px;
}
.style33 {font-size: 15px}
.style34 {
	font-size: 15px;
	font-weight: bold;
	font-family: "Courier New", Courier, monospace;
}
.style35 {font-size: 14px}
.style36 {font-size: 14px; font-family: "Courier New", Courier, monospace; }
-->
</style>
</head>

<body>
<div id="apDiv5"><img src="image/hp.jpg" alt="" width="109" height="71" /></div>
<div id="Layer7"><img src="image/main page/images/template83_r1_c6.jpg" alt="" width="765" height="77" /></div>


<div id="Layer8"><img src="image/main page/images/template83_r1_c8.jpg" alt="" width="445" height="77" /></div>
<div id="apDiv1">
  <ul id="MenuBar1" class="MenuBarHorizontal">
    <li><a href="#" class="style2 style3 MenuBarItemSubmenu style33">ดูข้อมูล</a>
    <ul>
          <li><a href="#" class="style2 style3"><span class="style6">รายการอุปกรณ์</span></a>
          <ul>
            <li><a href="hp.php" target="_parent" class="style35 style8 style3">HP</a></li>
            <li><a href="dell.php" target="_blank" class="style35 style8 style3">DELL</a></li>
            <li><a href="optimax.php" target="_blank" class="style35 style8 style3">OPTIMAX</a></li>
            <li><a href="acer.php" target="_blank" class="style35 style8 style3">ACER</a></li>
            <li><a href="atec.php" target="_blank" class="style35 style8 style3">ATEC</a></li>
            </ul>
        </li>
        <li><a href="totalcom.php" target="_blank"><span class="style6 style3"><strong>ยอดรวมอุปกรณ</strong></span>์</a></li>
      </ul>
    </li>
    <li><a href="#" class="MenuBarItemSubmenu style3 style33"><strong>เพิ่มข้อมูล</strong></a>
        <ul>
          <li><a href="#" class="MenuBarItemSubmenu"><span class="style13"><strong>รายการอุปกรณ</strong></span>์</a>
              <ul>
                <li><a href="add_hp.php" target="_parent" class="style12 style35">HP</a></li>
                <li><a href="add_dell.php" target="_parent" class="style12 style35">DELL</a></li>
                <li><a href="add_optimax.php" target="_parent" class="style12 style35">OPTIMAX</a></li>
                <li><a href="add_acer.php" target="_parent" class="style12 style35">ACER</a></li>
                <li><a href="add_atec.php" target="_parent" class="style12 style35">ATEC</a></li>
              </ul>
          </li>
        </ul>
    </li>
    <li>
      <div align="center"><a href="hp_edit.php" target="_parent" class="style34 style33">แก้ไข</a> </div>
    </li>
    <li><a href="index_admin.php" class="style3 style33"><strong>กลับหน้าหลัก</strong></a></li>
    <li>
      <div align="center"><a href="logout.php" class="style3 style33"><strong>ออกจากระบบ</strong></a></div>
    </li>
  </ul>
</div>
<p>&nbsp;</p>
<?
$host="localhost";
$username="root";
$password="3118";
$db="project";
$tb="hp";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้"); /* ทำการเลือกฐานข้อมูลก่อน */



$sql="Select id,brand,model,serial,name,type,location,maintainance From hp";
UNION
"Select id,brand,model,serial,name,type,location,maintainance From dell";
$db_query=mysql_db_query($db,$sql);
$num_rows=mysql_num_rows($db_query); /* นับ Reccord ที่พบ */
?>
  
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width="1242"border="0"align="center" bordercolor="#FF0000">
<tr>
<td width="125" height="21" bgcolor="#FF0000">
<div align="center" class="style6"><strong>ครุภัณฑ์</strong></div></td>
<td width="86" bgcolor="#FF0000">
<div align="center" class="style6"><strong>ยี่ห้อ</strong></div></td>
<td width="117" bgcolor="#FF0000">
<div align="center" class="style6"><strong>รุ่น</strong></div></td>
<td width="160" bgcolor="#FF0000">
<div align ="center" class="style6"><strong>S/N</strong></div></td>
<td width="90" bgcolor="#FF0000">
<div align="center" class="style6"><strong>ชื่อเครื่อง</strong></div></td>
<td width="116" bgcolor="#FF0000">
<div align="center" class="style6"><strong>ประเภท</strong></div></td>
<td width="185" bgcolor="#FF0000">
<div align="center" class="style6"><strong>สถานที่</strong></div></td>
<td width="275" bgcolor="#FF0000">
<div align="center" class="style6"><strong>ประวัติการซ่อม</strong></div></td>
<td width="50" bgcolor="#FF0000"><form action="" method="post" name="form1" class="style7 style17" id="form1">
  
    <div align="center">
      <div class="style6" id="apDiv4">ดูข้อมูล</div>
      </div>
</form>
  <span class="style24">ดูข้</span></td>
</table>
  <?
  $a=0;
  while($a<$num_rows)
  {
$result=mysql_fetch_array($db_query);
$id=$result[id];
$brand=$result[brand];
$model=$result[model];
$serial=$result[serial];
$name=$result[name];
$type=$result[type];
$location=$result[location];
$maintainance=$result[maintainance];
?>
<table width="1242"border="0"align="center">
<tr>
<td width="125">
<div align="center" class="style10 style32"><?echo"$id";?></div></td>
<td width="86">
<div align="center" class="style10 style32"><?echo"$brand";?></div></td>
<td width="117">
<div align="center" class="style10 style32"><?echo"$model";?></div></td>
<td width="160">
<div align="center" class="style10 style32"><?echo"$serial";?></div></td>
<td width="90">
<div align="center" class="style10 style32"><?echo"$name";?></div></td>
<td width="116">
<div align="center" class="style10 style32"><?echo"$type";?></div></td>
<td width="185">
<div align="center" class="style10 style32"><?echo"$location";?></div></td>
<td width="275">
<div align="center" class="style10 style32"><?echo"$maintainance";?></div></td>
<td width="50"><div align="center" class="style10 style32" onclick="MM_openBrWindow('loginfail.php','wintest','width=400,height=400')">ดูข้อมูล</div></td>
</tr>
</table>
<?
	$a++;
}
echo"<center><br>ค้นหาเจอ=$num_rows เครื่อง</center>";
mysql_close();
?>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"../SpryAssets/SpryMenuBarDownHover.gif", imgRight:"../SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>

Date : 2009-03-25 14:23:13 By : yutthana11
 


 

No. 5



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



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


หรือจะใช้คำสั่ง INNER JOIN ก้อได้นะครับ (เอาไว้เป็นทางเลือก)

รูปแบบ SQL ก้อ

"Select * From Table1 INNER JOIN Table2 ON Table1.ID=Table2.ID"

แต่ สองตารางนี้ ต้องมี Field ที่มีความสัมพันธ์กันนะครับ...
Date : 2009-03-25 14:23:16 By : noomna19
 


 

No. 6



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

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

Code
$sql="Select id,brand,model,serial,name,type,location,maintainance From hp
UNION
Select id,brand,model,serial,name,type,location,maintainance From dell";

Date : 2009-03-25 14:28:11 By : webmaster
 


 

No. 7

Guest


ได้แล้วครับ ขอบคุณครับ
Date : 2009-03-25 15:44:19 By : yutthana11
 


 

No. 8

Guest


ถ้าสองตารางมีไอดีเหมือนกันและไม่มีฟิลที่สัมพันกัน ทำงัยคับ จะเอามาทุก row เลย ช่วยด้วยคับ
Date : 2009-04-26 21:48:28 By : แบง
 


 

No. 9



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

สมาชิกที่ใส่เสื้อไทยครีเอท

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


ก้อเอา id มา join กัน
เพราะ id เหมือนกันไม่ใช่เหรอค่ะ
Date : 2009-04-27 09:10:02 By : paphun
 


 

No. 10

Guest


$sql="select * from tb_1,tb_2 where tb_1.id=tb_2.id";

แค่นี้ก็น่าจะได้แล้วนะครับส่วนวิธีดึงข้อมูลก็เช่น
while ($row = mysql_fetch_array($result)) {
$ตัวแปร1=$row["ชื่อฟิวดล์"
$ตัวแปร2=$row["ชื่อฟิวดล์"];
$ตัวแปร3=$row["ชื่อฟิวดล์"];
}

แล้วก็ echo"$ตัวแปร";แบบนี้จะได้หรือป่าวครับ ถ้าไม่ใช่ก็ ขออภัยนะครับ
Date : 2009-04-27 11:19:41 By : egat_uru@pich_uru
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : วิธีดึงข้อมูลจาก 2 ตาราง...โดยที่ฟิลล์เหมือนกันทุกอย่าง แค่ชื่อ ตารางไม่เหมือนกัน
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 01
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 อัตราราคา คลิกที่นี่