标签:
$user = M("sysUser");
$list=$user->find();
echo $user->getLastSql();
dump($list);
$user = M("sysDept");
$list=$user->find();
echo $user->getLastSql();
dump($list);
查询结果:
SELECT * FROM `tp_sys_user` LIMIT 1
array(1) {
[0] => array(5) {
["id"] => string(1) "1"
["user_name"] => string(6) "陈三"
["login_code"] => string(2) "cs"
["login_pwd"] => string(6) "123456"
["dept_id"] => string(1) "2"
}
}
SELECT * FROM `tp_sys_dept` LIMIT 1
array(2) {
[0] => array(3) {
["id"] => string(1) "1"
["dept_name"] => string(9) "研发部"
["remark"] => NULL
}
[1] => array(3) {
["id"] => string(1) "2"
["dept_name"] => string(9) "运营部"
["remark"] => NULL
}
}
两表关联(join为关联表,field()指定显示字段):
$list=$user->join(‘tp_sys_dept ON tp_sys_user.dept_id=tp_sys_dept.id‘)
->field(‘id‘,‘user_name‘,‘login_code‘,‘dept_id‘,‘dept_name‘)->find();
echo $user->getLastSql();
dump($list);
结果:
SELECT `user_name`,`login_code`,`login_pwd`,`dept_id` FROM `tp_sys_user` INNER JOIN tp_sys_dept ON tp_sys_user.dept_id=tp_sys_dept.id LIMIT 1
array(1) {
[0] => array(4) {
["user_name"] => string(6) "陈三"
["login_code"] => string(2) "cs"
["login_pwd"] => string(6) "123456"
["dept_id"] => string(1) "2"
}
}
tp_sys_user和tp_sys_dept表都有id列,id列被去掉了;那么尝试给列指定所在表
$list=$user->alias(‘a‘)->join(‘tp_sys_dept b ON a.dept_id=b.id‘)
->field(‘a.id‘,‘user_name‘,‘login_code‘,‘dept_id‘,‘dept_name‘)->find();
echo $user->getLastSql();
dump($list);
结果:
SELECT `id`,`user_name`,`login_code`,`login_pwd`,`dept_id` FROM tp_sys_user a INNER JOIN tp_sys_dept b ON a.dept_id=b.id LIMIT 1
bool(false)
同名列id已经指定了所在表,但不起作用;
$list=$user->field(‘a.id,user_name,login_code,dept_id,dept_name‘)
->table(‘tp_sys_user a,tp_sys_dept b‘)->where(‘a.dept_id=b.id‘)->find();
echo $user->getLastSql();
dump($list);
结果:
SELECT a.id,`user_name`,`login_code`,`dept_id`,`dept_name` FROM tp_sys_user a,tp_sys_dept b WHERE ( a.dept_id=b.id ) LIMIT 1
array(1) {
[0] => array(5) {
["id"] => string(1) "1"
["user_name"] => string(6) "陈三"
["login_code"] => string(2) "cs"
["dept_id"] => string(1) "2"
["dept_name"] => string(9) "运营部"
}
}
正常,尝试把where条件直接放在table中
$list=$user->field(‘a.id,user_name,login_code,dept_id,dept_name‘)
->table(‘tp_sys_user a,tp_sys_dept b where a.dept_id=b.id‘)->find();//将where条件放在table()中
echo $user->getLastSql();
dump($list);
结果:
SELECT a.id,`user_name`,`login_code`,`dept_id`,`dept_name` FROM tp_sys_user a,tp_sys_dept b where a.dept_id=b.id LIMIT 1
array(1) {
[0] => array(5) {
["id"] => string(1) "1"
["user_name"] => string(6) "陈三"
["login_code"] => string(2) "cs"
["dept_id"] => string(1) "2"
["dept_name"] => string(9) "运营部"
}
}
where条件直接在table()中也可以;但这样只能查内连接;
既然语句可以放在table()中,尝试用left join(左外连接)
$list=$user->field(‘a.id,user_name,login_code,dept_id,dept_name‘)
->table(‘tp_sys_user a left join tp_sys_dept b on a.dept_id=b.id‘)->find();
echo $user->getLastSql();
dump($list);
结果:
SELECT a.id,`user_name`,`login_code`,`dept_id`,`dept_name` FROM tp_sys_user a left join tp_sys_dept b on a.dept_id=b.id LIMIT 1
array(1) {
[0] => array(5) {
["id"] => string(1) "1"
["user_name"] => string(6) "陈三"
["login_code"] => string(2) "cs"
["dept_id"] => string(1) "2"
["dept_name"] => string(9) "运营部"
}
}
可以在table()中进行外连接;
传参查询:
$loginCode和$loginPwd为参数
$user->where("login_code=‘{$loginCode}‘ and login_pwd=‘{$loginPwd}‘")->find();
更多表查询内容,及常规用法请参考ThinkPHP手册
标签:
原文地址:http://www.cnblogs.com/wolongjv/p/5592010.html