อยากทำระบบค้นหา แบบหลายตารางครับ โดยผมทำการลอง inner join แล้ว แต่ติดตรงที่ว่าต้องการให้มันค้นหาในตารางอื่นที่ไม่เกี่ยวกับ ตารางหลัก แต่เกี่ยวกับตาราง fk อีกทีครับ
เช่น survey_detail เป็นตารางหลัก ในการค้นหา
ผมให้ survey_detail มี fk คือ surveying,และ tree ; survey_detail.sur_id = surveying.sur_id , survey_detail.tree_id=tree.tree_id
และมีตารางที่กี่ยวกับ ตาราง surveying คือ user ; surveying.user_id = user.user_id
ตารางที่เกี่ยวกับ tree คือ plant ; tree.pnt_id = plant.pnt_id
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."%' ");
<?
if(!isset($start)){
$start = 0;
$page=1;
}
$limit = '20';
if($strSearch=="Y"){
$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."%' ");
}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"){
$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."%' 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 417
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 435
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 457
$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."%' ");
$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."%' ");
$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."%' ");
$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."%' ");
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
<?
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
บันทัด 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."%' ");
survey_detail.sur_id=surveying.sur_id and survey_detail.tree_id=tree.tree_id
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 423
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 439
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\Web Project\page_managersurveydetail.php on line 462
Column 'sur_id' in where clause is ambiguous
Column 'tree_id' in where clause is ambiguous
มันต้องแก้ยังไงหรอครับ ซึ่งทั้ง2 ตารางผมใช้ชื่อเดียวกันอ่ะครับ ต้องไปแก้ชื่อฟิวมันหรือเปล่าครับ
มันขึ้นแบบเดิมครับ
คือ
Column 'tree_id' in where clause is ambiguous
Column 'pnt_id' in where clause is ambiguous
Column 'sur_id' in where clause is ambiguous
Column 'user_id' in where clause is ambiguous
ครับ Code (PHP)
$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 and
".$Search2." like '%".$Search."%' ")or die ( mysql_error() );
ผมไม่เข้าใจ " สีแดงรับค่ามาแบบไหน ต้องบอกว่า สร้าง 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" );
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 '%%'
ผลที่ออกมาครับ