foreachsql
㈠ sql語句怎麼循環查詢
selectf1fromtable1的結果集做為查詢條件循環查詢。
如:
set@a=selectf1fromtable1
foreach(@a)
{
select*fromtable2
wheref2=@a
}
㈡ 關於thinkphp中 foreach 裡面執行sql語句
原生sql查詢有
query()
和
execute()
兩個方法:
query():用於
sql
查詢操作,並返回符合查詢條件的數據集
execute():更新和寫入數據的
sql
操作,返回影響的記錄數
public function read(){
// 實例化一個空模型,沒有對應任何數據表
$ = m();
//或者使用 $ = new model();
$list = $->query("select * from user where uid<5");
if($list){
$this->assign('list', $list );
$this->display();
} else {
$this->error($->geterror());
}
}public function read(){
header("content-type:text/html; charset=utf-8");
// 實例化一個空模型,沒有對應任何數據表
$ = m();
//或者使用 $ = new model();
$num = $->execute("update user set email = '[email protected]' where uid=3");
if($num){
echo '更新 ',$num,' 條記錄。';
}else{
echo '無記錄更新';
}
}
㈢ php中foreach循環語句中可以使用sql查詢語句嗎比如
可以,但是你這樣做對資料庫的資源是很大的消耗,如果數據不多的話可以拿全集通過PHP進行篩選,或者進行in查詢