本文实例讲述了ThinkPHP采用原生query实现关联查询left join的方法。分享给大家供大家参考。具体如下:
thinkphp提供了join方法来实现关联查询,但是很不好用,还是用原生的方便,所以推荐大家用query方法:
复制代码 代码如下:
$Model = new Model();
$sql = "SELECT a.id, a.attr_name, a.attr_input_type, a.attr_type, a.attr_values, v.attr_value, v.attr_price ".
"FROM hh_typeattr AS a ".
"LEFT JOIN hh_siteattr AS v ".
"ON v.attr_id = a.id AND v.site_id = '$site_id' ".
"WHERE a.type_id = " . intval($type_id) ." OR a.type_id = 0 ".
"ORDER BY a.listorder, a.attr_type, a.id, v.attr_price, v.id";
$row = $Model->query($sql);
$sql = "SELECT a.id, a.attr_name, a.attr_input_type, a.attr_type, a.attr_values, v.attr_value, v.attr_price ".
"FROM hh_typeattr AS a ".
"LEFT JOIN hh_siteattr AS v ".
"ON v.attr_id = a.id AND v.site_id = '$site_id' ".
"WHERE a.type_id = " . intval($type_id) ." OR a.type_id = 0 ".
"ORDER BY a.listorder, a.attr_type, a.id, v.attr_price, v.id";
$row = $Model->query($sql);
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。