 |
สอบถามเรื่องการ ทำค้นหา php แบบหลายตาราง หน่อยครับ |
|
 |
|
|
 |
 |
|
join ตารางเอาข้อมูลออกมาทั้งหมดนั่นแหละ แล้ว select ครอบ select ที่ join อีกทีแล้วค่อย where หาค่าตามเงื่อนไข
|
 |
 |
 |
 |
Date :
2015-11-20 09:41:13 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
55555 ทำอะไรอะ
|
 |
 |
 |
 |
Date :
2015-11-20 16:28:04 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ค้นหา ครับ ผม join ตารางจากข้างบน ค้นหา
Code (PHP)
$Qtotal = mysql_query("SELECT *from survey_detail inner join
tree on survey_detail.tree_id = tree.tree_id inner join
plant on tree.pnt = plant.pnt_id inner join
surveying on survey_detail.sur_id = surveying.sur_id inner join
user on surveying.user_id = user.user_id
where ".$Search2." like '%".$Search."%' ");
|
ประวัติการแก้ไข 2015-11-20 16:34:39
 |
 |
 |
 |
Date :
2015-11-20 16:32:42 |
By :
sacool |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$Qtotal = mysql_query("SELECT *from survey_detail inner join
tree on survey_detail.tree_id = tree.tree_id inner join
plant on tree.pnt = plant.pnt_id inner join
surveying on survey_detail.sur_id = surveying.sur_id inner join
user on surveying.user_id = user.user_id
where ".$Search2." like '%".$Search."%' ");
เอามาแค่นี้
Code (SQL)
SELECT *from survey_detail inner join
tree on survey_detail.tree_id = tree.tree_id inner join
plant on tree.pnt = plant.pnt_id inner join
surveying on survey_detail.sur_id = surveying.sur_id inner join
user on surveying.user_id = user.user_id
where ".$Search2." like '%".$Search."%'
แก้เป็น
Code (SQL)
SELECT tb.* FROM (
SELECT *from survey_detail inner join
tree on survey_detail.tree_id = tree.tree_id inner join
plant on tree.pnt = plant.pnt_id inner join
surveying on survey_detail.sur_id = surveying.sur_id inner join
user on surveying.user_id = user.user_id
) tb
WHERE tb.".$Search2." like '%".$Search."%'
เอาไปใส่ใน ฟังชั่น
Code (PHP)
$Qtotal = mysql_query("SELECT tb.* FROM (
SELECT *from survey_detail inner join
tree on survey_detail.tree_id = tree.tree_id inner join
plant on tree.pnt = plant.pnt_id inner join
surveying on survey_detail.sur_id = surveying.sur_id inner join
user on surveying.user_id = user.user_id
) tb
WHERE tb.".$Search2." like '%".$Search."%' ");
ตัวอย่างนะคับ ไปประยุกต์เอาเองน้อ
|
 |
 |
 |
 |
Date :
2015-11-20 16:33:03 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าอยู่ใกล้นิ ตีตายเลยนะคับ มันพีคมากๆ ที่เขียนมาแบบนี้  
Code (PHP)
$Qtotal = mysql_query("$Qtotal = mysql_query("SELECT *from survey_detail inner join
tree on survey_detail.tree_id = tree.tree_id inner join
plant on tree.pnt = plant.pnt_id inner join
surveying on survey_detail.sur_id = surveying.sur_id inner join
user on surveying.user_id = user.user_id
where ".$Search2." like '%".$Search."%' ");
|
 |
 |
 |
 |
Date :
2015-11-20 16:34:52 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอา คิวรี่ไปรันเทสก่อน ว่าผ่านเปล่า
ถ้าผ่าน ลองใส่ค่าที่ตัวเสิชลงไป ว่าผ่านไหม
|
 |
 |
 |
 |
Date :
2015-11-20 19:05:51 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 9 เขียนโดย : progamer2000 เมื่อวันที่ 2015-11-20 19:05:51
รายละเอียดของการตอบ ::
ลองเอาไปรันใน phpmyadmin ขึ้นแบบนี้ครับ
Error
SQL query: Documentation
SELECT *
FROM (
SELECT *
FROM survey_detail
INNER JOIN tree ON survey_detail.tree_id = tree.tree_id
INNER JOIN plant ON tree.pnt_id = plant.pnt_id
INNER JOIN surveying ON survey_detail.sur_id = surveying.sur_id
INNER JOIN user ON surveying.user_id = user.user_id
)survey_detail, tree, plant, surveying
WHERE survey_detail
OR tree
OR plant
OR surveying LIKE '%"w"%'
LIMIT 0 , 30
MySQL said: Documentation
#1060 - Duplicate column name 'tree_id'
|
 |
 |
 |
 |
Date :
2015-11-20 22:42:08 |
By :
sacool |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอ่อ join ข้างในให้เสร็จไปก่อนซิครับ แล้วค่อยเอา select ครอบ ไม่ใช่ join ใน แล้ว มา join นอกต่อ
|
 |
 |
 |
 |
Date :
2015-11-21 00:10:07 |
By :
progamer2000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แนะนำ การใช้คำสั่ง inner join กับหลายตาราง
กรณี่ที่ใช้
Code (SQL)
from
tb1
inner join tb2 on ......
inner join tb3 on .....
จำทำการ คิวรี่ tb1 inner join tb2 ก่อน ได้ result มาค่อยเอาไป inner join กับ tb3
เป็นการทำ จากซ้ายไปขวา ตามลำดับ
ให้เปลี่ยน เป็น
Code (SQL)
from tb1, tb2, tb3
where tb1.id=tb2.fk_tb1 and tb2.id=tb3.fk_tb2
จะเป็นการ เปรียบเทียบ โดยการนำ tb1,tb2,tb3 มาเปรียบเทียบ พร้อมกัน
จะเร็วกว่าในกรณีข้อมูลเยอะๆ
ถ้าไม่มีความจำเป็นให้หลีกเลี่ยงวิธีที่ 1 (มันง่ายต่อการเข้าใจ แต่มันช้าในการทำงานจริง)
แต่ถ้ามีแค่ 2 ตาราง ก็โอเคเลยครับ
ปล. อันนี้เป็นวิธีการทำงานของmysql เวอร์ชั่นเก่าๆ ซึ่งเมื่อก่อนเขาแจก source มาให้แกะเล่นได้
แต่รุ่นใหม่นี้ไม่รู้เขาแก้ไขอะไรไปบ้างขนาดไหนนะครับ ไม่ได้อ่านมาหลายเวอร์แล้ว มันเปลี่ยนเร็วจริงๆ
ว่าจะหาเวลาว่างไปอ่าน document อยู่แต่ยังไม่มีเวลา + ขี้เกียจ 5555
|
 |
 |
 |
 |
Date :
2015-11-21 04:05:58 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 12 เขียนโดย : NewbiePHP เมื่อวันที่ 2015-11-21 04:05:58
รายละเอียดของการตอบ ::
Code (PHP)
<?
if(!isset($start)){
$start = 0;
$page=1;
}
$limit = '20';
if($strSearch=="Y"){
$Qtotal = mysql_query("SELECT * FROM survey_detail,surveying,tree,plant,user
where survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id and tree.pnt_id=plant.pnt_id and surveying.user_id = user.user_id ".$Search2." like '%".$Search."%' ");
}else{
$Qtotal = mysql_query("SELECT * FROM `survey_detail`A LEFT JOIN
`tree` B ON A.tree_id = B.tree_id LEFT JOIN
`plant` C ON B.pnt_id = C.pnt_id LEFT JOIN
`surveying` D ON A.sur_id = D.sur_id LEFT JOIN
`user` E ON D.user_id = E.user_id LEFT JOIN
`family` F ON C.family_id = F.family_id
WHERE A.approve_status = '0' ORDER BY sur_detail_id DESC");
}
$total = mysql_num_rows($Qtotal);
if($strSearch=="Y"){
$Query = mysql_query("SELECT * FROM survey_detail,surveying,tree,plant,user
where survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id and tree.pnt_id=plant.pnt_id and surveying.user_id = user.user_id ".$Search2." like '%".$Search."%' order by sur_detail_id DESC LIMIT $start,$limit");
}else{
$Query= mysql_query("SELECT * FROM `survey_detail`A LEFT JOIN
`tree` B ON A.tree_id = B.tree_id LEFT JOIN
`plant` C ON B.pnt_id = C.pnt_id LEFT JOIN
`surveying` D ON A.sur_id = D.sur_id LEFT JOIN
`user` E ON D.user_id = E.user_id LEFT JOIN
`family` F ON C.family_id = F.family_id
WHERE A.approve_status = '0' ORDER BY sur_detail_id DESC LIMIT $start,$limit");
}
$totalp = mysql_num_rows($Query);
$xx=$totalp+$start;
$yy=$start+1;
echo "ข้อมูลที่ ".$yy." ถึงข้อมูลที่ ".$xx;
printf(" | จากทั้งหมด %d รายการ ",$total);
printf(" | หน้าที่ %d <br />",$page);
?>
แบบนี้ก็ยัง error อย่เหมือนเดิมครับ
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 442
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 458
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 481
|
 |
 |
 |
 |
Date :
2015-11-21 18:59:47 |
By :
sacool |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บันทัด 10. ตกสีแดง
where survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id and tree.pnt_id=plant.pnt_id and surveying.user_id = user.user_id And ".$Search2." like '%".$Search."%' ");
*************************************
บันทัด 24 ก็แบบเดียวกัน
|
 |
 |
 |
 |
Date :
2015-11-21 20:36:24 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$Query=mysql_query(....) or die ( mysql_error() )
ใส่สีแดงเข้าไปด้วยจะได้รู้ว่า error อะไร
mysql_num_rows( $Query ) error เพราะ คำสั่งคิวรี่มันไม่ถูกต้อง เลยทำให้ได้ค่า false ออกมา
พอเอาไปใส่ mysql_num_rows( false ) มันก็error argument
|
 |
 |
 |
 |
Date :
2015-11-21 23:11:43 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่ชื่อตารางให้ field ด้วย ในกรณีที่มีชื่อ field ซ้ำกัน
|
 |
 |
 |
 |
Date :
2015-11-22 06:24:12 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จริงๆ ไม่ต้องถามก็ได้มั้งครับแบบนี้ เอาไปลองก็รู้ผลแล้วล่ะครับว่าได้ไม่ได้
|
 |
 |
 |
 |
Date :
2015-11-22 12:08:24 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้เพราะอะไรล่ะครับ ผมจะรู้ไหมเนี่ย คุณเป็นคนทดลองบอกแค่ไม่ได้
555555
ไม่ได้มีตาทิพย์นะครับ ทดลองแล้วก็สรุปให้รู้ด้วยสิครับ
|
 |
 |
 |
 |
Date :
2015-11-22 15:36:35 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มี่ที่เดียวที่น่าจะผิด
$Qtotal = mysql_query($sql="SELECT * FROM survey_detail,surveying,tree,plant
.......
".$Search2." like '%".$Search."%' ")or die ( mysql_error() . "<br>$sql" );
สีแดงรับค่ามาแบบไหน ต้องบอกว่า สร้าง value ไว้แบบไหนในหน้าเสริจ ดีกว่าครับ
เพิ่มสีน้ำเงินเข้าไปด้วยครับ จะได้เห็น statement ที่ทำไว้
|
 |
 |
 |
 |
Date :
2015-11-22 20:21:59 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 24 เขียนโดย : NewbiePHP เมื่อวันที่ 2015-11-22 20:21:59
รายละเอียดของการตอบ ::
ผมไม่เข้าใจ " สีแดงรับค่ามาแบบไหน ต้องบอกว่า สร้าง value ไว้แบบไหนในหน้าเสริจ " ครับ ก็เลยเอา ฟอร์มที่ส่งค่ามา $search2 มาให้พี่ดูครับ
ผมเลยไม่ได้เปลี่ยนแค่ $search2 ครับ หรือให้ผมลองใส่ค่าตรง ๆ เข้าไป แทน $search2 เช่น $pnt_id ผมก็ลองแล้วครับ
มันออกมาแบบนี้ครับ
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like '%%'' at line 3
SELECT * FROM survey_detail,surveying,tree,plant,user where survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id and tree.pnt_id=plant.pnt_id and surveying.user_id = user.user_id and like '%%'
Code (PHP)
$Qtotal = mysql_query($sql="SELECT * FROM survey_detail,surveying,tree,plant,user
where survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id and
tree.pnt_id=plant.pnt_id and surveying.user_id = user.user_id and ".$Search2." like '%".$Search."%' ")or die ( mysql_error()."<br>$sql" );
Code (PHP)
<form name="form1" method="post" action="?p=manager-surveydetail&show=OK&strSearch=Y">
<table width="98%" >
<tr>
<td width="50%"><div align="right">ค้นหา :
<select name="Search2" class="inp-form355">
<option value="sur_detail_id" <? if($Search2=="sur_detail_id"){ ?>selected<? }?>>รหัสการสำรวจ</option>
<option value="tree_id" <? if($Search2=="tree_id"){ ?>selected<? }?>>รหัสการสำรวจ</option>
<option value="sur_id" <? if($Search2=="sur_id"){ ?>selected<? }?>>รหัสทะเบียนสำรวจ</option>
<option value="user_id" <? if($Search2=="user_id"){ ?>selected<? }?>>รหัสผู้สำรวจ</option>
<option value="sur_time" <? if($Search2=="sur_time"){ ?>selected<? }?>>เวลาสำรวจ</option>
<option value="pnt_vernac_name" <? if($Search2=="pnt_vernac_name"){ ?>selected<? }?>>ชื่อพื้นเมือง</option>
<option value="sur_locality" <? if($Search2=="sur_locality"){ ?>selected<? }?>>สถานที่สำรวจ</option>
<option value="user_name" <? if($Search2=="user_name"){ ?>selected<? }?>>ผู้สำรวจ</option>
<option value="pnt_id" <? if($Search2=="pnt_id"){ ?>selected<? }?>>รหัสข้อมูลพืช</option>
<option value="approve_status" <? if($Search2=="approve_status"){ ?>selected<? }?>>สถานะ</option>
</select>
</div></td>
<td width="64%"><input name="Search" type="text" size="20" value="<? echo $Search?>"onFocus="this.value = '';" class="inp-form300" placeholder="Search">
<input name="submit" type="submit" class="btn" value="Search" > </td>
</tr>
<tr>
<td><div align="center">
<input type="radio" name="status" id="status" value="1">
อนุมัติแล้ว
<input type="radio" name="status" id="status" value="2">
ยังไม่อนุมัติ</div></td>
<td> </td>
</tr>
</table>
</form>
Column 'sur_id' in where clause is ambiguous
SELECT * FROM survey_detail,surveying,tree,plant,user where survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id and tree.pnt_id=plant.pnt_id and surveying.user_id = user.user_id and sur_id like '%%'
ผลที่ออกมาครับ
|
ประวัติการแก้ไข 2015-11-22 22:15:45
 |
 |
 |
 |
Date :
2015-11-22 22:10:33 |
By :
sacool |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<option value="sur_detail_id" <?
ไม่มีชื่อตารางใช่ไหมครับ พอเอาไปใช้ มันก็ไม่มีชื่อตารางเหมือนกัน
ดังนั้นควรใส่ชื่อตารางเข้าไปด้วยครับ
อาจจะใส่ที่คิวรี่เลยก็ได้ถ้า ฟีลด์ทั้งหมดนี้มันมีอยู่ในตารางเดียว
แต่ถ้าหาจากหลายตาราง ก็ใส่ใน value ของ option ไปเลยครับ
value="tablename.sur_detail_id"
ส่วนข้างล่าง ตัวแปรไม่มาด้วย ถึงกลายเป็น %% ถ้ามีตัวแปรควรจะมีอะไรอยู่กลาง %xxx%
user.user_id and like '%%'
|
ประวัติการแก้ไข 2015-11-22 23:33:30
 |
 |
 |
 |
Date :
2015-11-22 23:29:59 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่่มปุ่มเป็น option อีกตัวไปเลยครับ อย่าต้องให้พิมพ์เลยครับ คนพิมพ์ส่วนมากจะมั่ว
แล้วจะหาคำค้นไม่เจอ
|
 |
 |
 |
 |
Date :
2015-11-22 23:55:42 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|