以一个 fh_joinactivity 表和 fh_activity 表联查为例:
$id = $_SESSION['user'] ? $_SESSION['user']:'0';
第一种方式:
$data = M('joinactivity as a')->join('fh_activity as b on b.a_id = a.a_id')->where('a.u_id ='.$id)->field('b.a_title, b.a_time')->select();
注意:前面的表不加前缀,后面的join要加上前缀,并且$id要存在,否则会出错,为了避免出错,$id不存在时,我这里默认给了0,field()是要查找的结果字段,也可以不设置,全部字段查找出来
效果:
array (size=2)
0 =>
array (size=2)
'a_title' => string '娲诲姩浜�' (length=9)
'a_time' => string '1486141800' (length=10)
1 =>
array (size=2)
'a_title' => string '娲诲姩浜�' (length=9)
'a_time' => string '1486141800' (length=10)
第二种:
$data = M()->table('fh_joinactivity as a')->join('fh_activity as b on b.a_id = a.a_id')->where('a.u_id ='.$id)->select();
注意:这里的表都加前缀,并且$id要存在,否则会出错,为了避免出错,$id不存在时,我这里默认给了0,field()是要查找的结果字段,不设置,全部字段查找出来
效果:
array (size=2)
0 =>
array (size=18)
'ja_id' => string '12' (length=2)
'ja_name' => string '鍚嶅瓧涓€' (length=9)
'ja_tel' => string '13578945612' (length=11)
'a_id' => string '21' (length=2)
'ja_addtime' => string '1459999999' (length=10)
'u_id' => string '1' (length=1)
'is_read' => string '0' (length=1)
'a_title' => string '娲诲姩浜�' (length=9)
'a_time' => string '1486141800' (length=10)
'a_addr' => string '娲诲姩鍦扮偣浜�' (length=15)
'a_content' => string '<p>娲诲姩浜�<br/></p>' (length=39)
'a_num' => string '13' (length=2)
'a_joinnum' => string '2' (length=1)
'a_img' => string 'Upload/activity/1486089587.png' (length=30)
'a_author' => string '1' (length=1)
'is_show' => string '1' (length=1)
'is_del' => string '0' (length=1)
'add_time' => string '1486089587' (length=10)
1 =>
array (size=18)
'ja_id' => string '13' (length=2)
'ja_name' => string '鍚嶅瓧浜�' (length=9)
'ja_tel' => string '13578948888' (length=11)
'a_id' => string '21' (length=2)
'ja_addtime' => string '1469999999' (length=10)
'u_id' => string '1' (length=1)
'is_read' => string '0' (length=1)
'a_title' => string '娲诲姩浜�' (length=9)
'a_time' => string '1486141800' (length=10)
'a_addr' => string '娲诲姩鍦扮偣浜�' (length=15)
'a_content' => string '<p>娲诲姩浜�<br/></p>' (length=39)
'a_num' => string '13' (length=2)
'a_joinnum' => string '2' (length=1)
'a_img' => string 'Upload/activity/1486089587.png' (length=30)
'a_author' => string '1' (length=1)
'is_show' => string '1' (length=1)
'is_del' => string '0' (length=1)
'add_time' => string '1486089587' (length=10)
如果有多个表,继续在table 后面加 join 就行了,这里没有处理展示编码问题(不是这里的重点)