联系电话:020-28187900

K9标签基础篇:数据查询【关联读取】

发表时间:2012-08-06 阅读: 3431

{@res.join table1='%s_content' table2='%s_user' field1='id,title,url' field2='username' on='userid' where='t1.status=1' order='t1.id desc'}

读取表1的时候,同时读取被关联的表里的其他内容,比如上例循环显示的是content表里的数据,同时根据content表里的userid字段关联读取附表user里的username字段,用来显示对应的用户名。

读取返回值是二维数组,赋给数组变量res:
{#foreach ($res as $rs)}
内容标题:{$rs[title]}
用户名是:{$rs[username]} 
链接地址:{$rs[url]}<br/>
{#endforeach}

属性值,标注*的是必须填写的属性值,+代表必须同时指定或同时不指定
* table1:要读取的主数据库表名
* table2:被关联读取的附数据库表的名称
+ field1:主数据库表里的字段列表
+ field2:附数据库表里的字段列表
* on:关联两个表用的字段,这个字段两个表必须得存在
where:条件检索,主表字段前面需要补充t1.,附表是t2
order:排序方式,主表字段前面需要补充t1.,附表是t2
number:调用数量,如果跳过10条读取5条,则: number='10, 5'

例子:

调用模型为1且分类为1的文章8条

{@res.join table1='%s_content_categroy_bind' table2='%s_content' field2='title,url' on='id' where='t2.mid=1 and t2.status=1 and cid=1' order='t2.id desc' number='8'}
{#foreach($res as $rs)}
<p><a href="{$rs[url]}" title="{$rs[title] code='quote'}">{$rs[title]}</a></p>
{#endforeach}
on=id:表table1和table2关联的字段为id
mid=1:模型为1
cid=1:分类为1

转载请注明出处:唯众网络


相关教程 【更多】

唯众网络