php查詢多表
PHP+Mysql多條件多值查詢示例代碼:
index.html代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>SQL多條件查詢示例</title></head><body><form method="post" action="deal.php"><h1>房屋出租</h1>房屋類型:<select name="type"><option value="1">一居室</option><option value="2">二居室</option><option value="3">三居室</option></select>面積:<input name="area" type="text"/>地址:<input name="addr" type="text"/><input name="btn" type="submit" value="搜索" /></form></body></html>
deal.php文件:
<?php//連接資料庫$conn=mysql_connect("localhost","root",""); //選擇資料庫$db=mysql_select_db("資料庫名"); //接收 參數$type=$_POST['type'];$area=$_POST['area'];$addr=$_POST['addr']; //SQL語句主題$query="select * from room where "; //根據條件和傳的值拼接sql語句//判斷面積不為空if($type!=""){ //然後根據具體面積分情況拼接 switch($type){ case 1: //一居室 $query.=" room_type=1"; break; case 2: $query.=" room_type=2"; break; case 3: $query.=" room_type=3"; break; }} //面積if($area!=""){ $query.=" and area ={$area}";} //地址if($addr!=""){ $query.=" and addr like '%{$addr}%'"; //地址} //執行查詢$result=mysql_query($query); //遍歷結果echo "搜搜結果如下:";while($row=mysql_fetch_array($result)){ echo "地址:".$row['addr']; echo ""; echo "面積:".$row['area']; echo ""; echo "居室:".$row['type']; echo ""; echo "價格:".$row['addr']; echo ""; //等等} ?>
Ⅱ PHP如何實現多表聯查並且將特定標簽的內容替換成別的
先查詢出表a中question_detial欄位,再通過php正則匹配獲取標簽[attach]1[/attach]中的ID,通過該ID查詢表b,獲取到file_location欄位,然後通過php(str_replace)替換掉question_detial中的標簽[attach]1[/attach]
$sql='select*from`表A`where條件';
$res=mysql_query($sql);
$data=mysql_fetch_assoc($res);
$question_detial=$data['question_detial'];
preg_match_all('/[attach]([0-9]*)[/attach]/',$question_detial,$match);
if(isset($match[1])&&$match[1]){
$str_search=null;
$str_replace=null;
foreach($match[1]as$key=>$val){
$str_search[$key]='[attach]'.$val.'[/attach]';
$str_replace[$key]='';
$sql_b="selectfile_locationfrom`表B`whereid=".$val;
$res_b=mysql_query($sql_b);
$row_b=mysql_fetch_assoc($res_b);
$str_replace[$key]='<imgsrc="'.$row_b['file_location'].'"/>';
}
$data['question_detial']=str_replace($str_search,$str_replace,$question_detial);
}
print_r($data);
Ⅲ PHP 多表關聯查詢怎麼寫
你是三個表嗎?
order 的orderid 對應order_goods的orderid
order_goods的goodsid 對應 goods的id
然後你是想通過產品ID查詢訂單出來?
SELECT * FROM order WHERE orderid IN (select og.orderid from order_goods og left join goods g ON og.goodsid = g.id)
Ⅳ Thinkphp3.2怎麼寫多表查詢語句
$Model = M('Artist');
$Model->join('think_work ON think_artist.id = think_work.artist_id')
->join('think_card ON think_artist.card_id = think_card.id')->select();
Ⅳ thinkphp中怎麼多表查詢
M('user')->field('u.*')->join('uleftjoinnewnonn.userid=u.userid')->select();
echoM('user')->getLastSql();//查看上一條sql語句
Ⅵ PHP 多表聯查
b, c, d 三張表的 name 你給出了a表的四個欄位,第四個欄位是幹嘛的?
userId 不加上了,不知道你這個欄位是幹嘛的。 b, c, d 三張表中的name欄位對應著a表中的wareId, goodsId, wareManager
SELECT
*
FROM
a,b,c,d
WHERE
a.wareId=b.name
AND
a.goodsId=c.name
AND
a.wareManager=d.name;
Ⅶ PHP MySQL 如何同時查詢兩張表
直接表連接查詢就可以了
select * from fs_sheet1_info inner join fs_sheet2_hr on fs_sheet1_info.ID = fs_sheet2_hr.ID
Ⅷ php 的多表查詢 怎麼做
很簡單啊,可以選擇JION關鍵字。
比如<?php
session_start();
include "conn/conn.php";
$s_sqlstr="select * from xs inner jion xs_kc on xs.xh=xs_kc.xh"order by xhDesc";
$s_rst = $conn->execute($s_sqlstr);
?>
這就實現了兩個表的查詢,你也可以加別名,這樣更方便書寫
Ⅸ php多表查詢問題。
table1 :表示表一
table3 :表示表三
select b.(table3欄位),b.(table3欄位) from table1 as a,table3 as b where a.class_id = b.class_id limit 1
Ⅹ php如何一起查詢多個資料庫的所有表
下面的代碼可以查詢單個資料庫的所有表的指定的欄位內容,如何才能實現多個資料庫一起查詢相同欄位的內容,每個資料庫欄位都一樣,表都是100+個。並且下面的代碼雖然能查詢單個資料庫所有表的內容,但是查詢一次耗費時間很長,該怎麼樣優化才能加快速度,不然假設多個資料庫一起查詢實現了,該會變得多卡。
$i=1; //初始一個變數iwhile($i<=100) //當變數i小於等於100時都執行{ $query ="select * from 表".$i." where 欄位1=". $textfield; $row =mssql_query($query); $i++; //變數i遞增運算//輸出查詢結果while($list=mssql_fetch_array($row)){ //print_r($list);echo '賬號:',$list['欄位1'];echo '--〉昵稱:',$list['欄位2'];echo '--〉密碼:',$list['欄位3']; echo '</br>';}} }