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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/bbc59b79aa5ed810e0dd8329be900689.html