thinkphp 查詢 語句問題
使用對象方式來查詢 (這里以stdClass內置對象為例)
$User = M("User"); // 實例化User對象
// 定義查詢條件
$condition = new stdClass();
$condition->name = 'thinkphp';
$condition->status= 1;
$User->where($condition)->select();最后生成的SQL語句和上面一樣
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1使用對象方式查詢和使用數組查詢的效果是相同的,并且是可以互換的,大多數情況下,我們建議采用數組方式更加高效,后面我們會以數組方式為例來講解具體的查詢語言用法。
thinkphp查詢語句
$array = 。
; % 數組array的內容$where['type'] = 1;$where['s_id'] = array('not in',$array);$map['_complex'] = $where;$map['t_id'] = array('in',$array);$map['_logic'] = 'or';$con['_complex'] = $map;$con['id'] = 1;M("Model")->where($con)->select();。
thinkPHP MySQL查詢語句生成問題
使用快捷查詢
加入$map['_logic'] = 'OR';
或
$map['id|receiverid'] = array(session('uid'),array('in', implode(",",$idset)),'_multi'=>true);
以上方法等效。
當且與或并存時可以用復合查詢,如以下多了一個name
$map['receiverid'] = session('uid');
$map['id'] = array('in', implode(",",$idset));
$map['_logic'] = 'OR';
$where['name'] = 'abc';
$where['_complex'] = $map;
M('message') -> where($where)->select();
將生成:
WHERE (`receiverid` = 1 OR `id` IN ('13','12','10','9')) AND `name`='abc'
thinkphp 查詢 and or and
select * from x where (id=1 and name="2") or (id=1 and name="3");
可以這樣寫
select * from x where id=1 and name in("2","3");
in 里面可以有很多參數,如果in里面數據量比較大,可以用數組存儲,如
$array = (1,2,3,4,5);
$str = '';
foreach($array as $val){
$str .= $val . ','; //將值后面拼接一個逗號
}
$array = rtrim($str,','); //將數組變成合法的字符串
select * from x where id=1 and name in($str);
這樣也是批量操作的一個方法
如果這些不是你想要的,請將問題描述清楚,謝謝
轉載請注明出處華閱文章網 » thinkphpin查詢語句