1、原生查询示例: |
1 $Model = new Model(); |
2 $sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id ' . $map . ' order by a.id ' . $sort . ' limit ' . $p ->firstRow. ',' . $p ->listRows; |
3 $voList = $Model ->query( $sql ); |
2、join()方法示例: |
1 $user = new Model( 'user' ); |
2 $list = $user |
->join( 'RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' ); //默认左连接 |
->join(); //多个 |
3、table()方法示例: |
table( '表名' => '别名' ) |
$data =M()->table( array ( 'user_status' =>status, 'user_profile' =>profile))->where( 'stats.id = profile.typeid' )->field( 'stats.id as id, stats.display as display, profile.title as title,profile.content as content' )->order( 'stats.id desc' )->select();; |
$list = $user ->table( 'user_status stats, user_profile profile' )->where( 'stats.id = profile.typeid' )->field( 'stats.id as id, stats.display as display, profile.title as title,profile.content as content' )->order( 'stats.id desc' )->select(); |
//多表查询:union查询 union('string array',true/flase); |
$data =M( 'User' )->field( 'user_name,id' )->union( 'select user_name,id from mk_user2' )->select(); //字段顺序一定要一样 |
$data =M( 'User' )->field( 'user_name,id' ) |
->union( array ( 'field' =>user_name, 'field' =>id, 'table' => 'mk_user2' ),true) |
->union( array ( 'field' =>user_name, 'field' =>id, 'table' => 'mk_user3' ),true) |
->select(); |
//过滤查询distinct |
$data =M( 'User)->distinct(true)->field(' score ')->order(' score asc')->select(); |