用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

tp多表查询

2015-03-28 作者: 云代码会员举报

[php]代码库

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();


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...